useChangeObserver
SourceCall a function whenever a value changes.
This works by monitoring a value passed in and tracking it's last value. Whenever the value changes the provided callback will be called with the last and current value.
export default function Example() {const [count, setCount] = useState(0);useChangeObserver(count, () => {console.log(`Changed from ${prev} to ${next}.`));}return <>Count: {count}<button onClick={() => setCount(c => c+1)}>+1</button></>;}
Parameter | Type | Description | |
---|---|---|---|
* | value | T | The value to monitor for changes. This can be any type but for complex
types you will need to provide your own |
* | onChange | Function | The function to call when |
options.disabled | boolean | If true then no changes will be detected. When this changes from true to false the callback won't be called until the next change in value. This is useful for disabling the callback when no value is yet available eg. when waiting for first response from an API. | |
options.isEqual | Function | Function to determine equality between items. If not provided the default will do shallow
equality checks with specific support for an |
Has no return value