Look at this demo
Is there a bug?
What is the current behavior?
When you click the minus button until value to -1, the # 1 effect will not be triggered, but the dependency has changed.
What is the expected behavior?
The # 1 effect will be trigger when the value changes from 0 to -1锛宩ust like # 2 effect
Using ref.current
in a dependency array is going to be problematic. @gaearon explains why in this issue: https://github.com/facebook/react/issues/14387#issuecomment-503616820
It was triggered when the value changed, but the code [ref.current]
was executed BEFORE the mutation by ref={ref}
, while console.log(ref.current)
was executed AFTER (because useEffect
is executed after render).
Execution of useEffect(() => console.log(ref.current), [ref.current])
code can be imagined as follows:
1st render: useEffect(() => console.log('num 1'), [null]
)
2nd render: useEffect(() => console.log('num 0'), [element with num 1]
)
3rd render: useEffect(() => 'the element did NOT change in previous render', [element with num 0]
)
4th render: useEffect(() => console.log(null), [null]
)
This is expected. Dependencies should only include values that participate in top-down React data flow. Such as props, state, and what you calculate from them. Ref containers are fine too (since they can be passed down).
However, it doesn鈥檛 make sense to add ref.current
as a dependency. For the same reason it doesn鈥檛 make sense to add window.myVariable
. When it updates, React won鈥檛 know about it, and won鈥檛 update your component.
In your case, you either want to use state instead. Or, if you want to be notified about <Something ref={ref} />
getting attached and detached (for example, to measure DOM nodes), you want to use a callback ref instead.
https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node
if you want to be notified about
<Something ref={ref} />
getting attached and detached (for example, to measure DOM nodes), you want to use a _callback ref_ instead.
Using this pattern, how to know if ref is getting detached? (similar to the behaviour of the function returned from useEffect
callback). We can check if node !== null
, but it only checks node existence, and it exists before detach as well as after attach. The guide notes that "the callback ref will be called only when the component mounts and unmounts", but how to distinguish between mount and unmout?
Most helpful comment
This is expected. Dependencies should only include values that participate in top-down React data flow. Such as props, state, and what you calculate from them. Ref containers are fine too (since they can be passed down).
However, it doesn鈥檛 make sense to add
ref.current
as a dependency. For the same reason it doesn鈥檛 make sense to addwindow.myVariable
. When it updates, React won鈥檛 know about it, and won鈥檛 update your component.In your case, you either want to use state instead. Or, if you want to be notified about
<Something ref={ref} />
getting attached and detached (for example, to measure DOM nodes), you want to use a callback ref instead.https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node