-
Notifications
You must be signed in to change notification settings - Fork 47.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useEffect will unpredictable when depends on ref (useRef) #16121
Comments
Using |
It was triggered when the value changed, but the code Execution of 1st render: |
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’t make sense to add In your case, you either want to use state instead. Or, if you want to be notified about https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node |
Using this pattern, how to know if ref is getting detached? (similar to the behaviour of the function returned from |
@gaearon, what about when you have a list of dynamically added refs (say an array of refs for a component's child). How could one create callback refs like is described here? What my approach looks like now (won't trigger useEffect's deps array): const childrenCount = React.Children.count(children);
const [childRefs, setChildRefs] = Array.from({ length: childRefs }).map(createRef);
return React.Children.map(children, (child, index) =>
React.cloneElement(child, {
ref: childRefs[index],
})
); |
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,just like # 2 effect
The text was updated successfully, but these errors were encountered: