const useSomeHook = ({number}) => {
const [newNumber, setNewNumber] = useState(0)
useEffect(() => {
setNewNumber(number + 1)
}, [number])
}
const SomeComponent = ({number, value, ...restProps}) => {
useSomeHook({number})
return <div>{number}</div>
}
Let's imagine I have this case. Each time when in SomeComponent
come some new prop, it will call my useSomeHook
hook, but I want to prevent it. I want to call it only when the number
is changed (memoize
it). In other cases, don't touch it. But I haven't found any solving with this case. Could you help me solve this issue?
useEffect
hook withinuseSomeHook
will only be invoked when the dependencynumber
changes. – RecordinguseEffect
will trigger subsequent invocation of the callback you pass in as the first argument. To address your ask, you can do two things: 1. Add in-depth equality check as the second param (this requires some work), and/or 2. Add a condition in the callback; ex:if (obj.a !== obj.b) callback()
– WaterscapeuseRef
instead ofuseState
– Katlin