-
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
Refs callback is called twice on every render, even when component instance is reused #8619
Comments
I think I figured this out, using a clue in the code comments of
If I change my usage to
…I then get the expected result! What was happening above is, during every render, a new anonymous function is instantiated for the So the workaround is to pass some sort of stable callback, like a pre-bound method or a long-lived function instance. I will leave this open, because this "handoff" behavior is not documented at https://facebook.github.io/react/docs/refs-and-the-dom.html, nor is this implication when using inline functions highlighted (as a potential pitfall). |
I have a question, can help me? There are two sub-components, radio and select, in the parent container through the switch call the two components, and then need to traverse the component in the parent container call to get the number of components within the method. I try to write in the following way, is wrong. container:
|
Also ran into this. It's particularly a problem when a parent component maintains an array of refs to its children. One might expect this code to work properly:
However, if a child goes away, so that there are only |
Has been resolved. Thank you. Perhaps this.refs on behalf of the key |
You might want to take a look at #9328 which explains why React behaves like this. |
Closing as a duplicate of #9328 (it is the other way around, but that issue got more attention). |
@gaearon Works for me, looks like this behavior did get documented as a caveat since I filed this; that the main reason I had left this one open anyway. Thanks! For those finding this issue, an in-depth explanation of the (admittedly surprising at first) logic is here: #9328 (comment) |
Do you want to request a feature or report a bug?
Bug, perhaps just in documentation.
What is the current behavior?
The documentation for Refs and the DOM says:
But if I create a stateful component like:
and use it via
<ScrollHelper ref={(obj) => console.log("ScrollHelper ref", obj)}/>
within an app, I do not see the behavior documented, but rather the following pattern of logs:What is the expected behavior?
According to the docs, I would expect my
refs
callback to be called exactly two times during the events above — once (with the component) after the "ScrollHelper componentDidMount" and once (withnull
) before the "ScrollHelper componentWillUnmount". Instead it gets "reset" basically every time the page is re-rendered — even though it's pretty clearly ending up with the same component instance each time! (I verified this is true via some additional code as well, but should be evident from the lack of additional "componentDidMount" logs anyway.)Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
react@15.4.1, no idea what the behavior was earlier.
The text was updated successfully, but these errors were encountered: