Skip to content

Commit

Permalink
fix(use-controllable): call onChange only if value changed
Browse files Browse the repository at this point in the history
  • Loading branch information
dodas committed Feb 17, 2021
1 parent 11ac4f9 commit 623e782
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/dirty-radios-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@chakra-ui/hooks": patch
---

`useControllableState`: The `onChange` callback will be called only if the new
value isn't equal to the current one.
6 changes: 6 additions & 0 deletions packages/hooks/src/use-controllable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ export function useControllableState<T>(props: UseControllableStateProps<T>) {
const updateValue = React.useCallback(
(next: React.SetStateAction<T>) => {
const nextValue = runIfFn(next, value)

if (nextValue === value) {
return
}

if (!isControlled) {
setValue(nextValue)
}

handleChange(nextValue)
},
[isControlled, handleChange, value],
Expand Down

0 comments on commit 623e782

Please sign in to comment.