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
componentDidUpdate fires before ref is set in React 16 #12034
Comments
It should perhaps be noted that this can be fixed with a portal: export default class BodyEnd extends React.Component {
constructor(props) {
super(props);
this.el = document.createElement('div');
}
componentDidMount() {
document.body.appendChild(this.el);
}
render() {
return ReactDOM.createPortal(
this.props.children,
this.el,
);
}
} But the above was working in React 15.6, so it seems like a regression/backwards-compatibility bug. |
Major versions don’t have to be backwards-compatible (that’s why they’re majors 😉). I haven’t looked into your example in detail but my hunch is this is a known intentional limitation that comes up with nested |
@gaearon Right, but you said yourself,
I assume you're referring to this post. Perhaps I'm mistaken, and one of those bullet points covers this scenario, but otherwise I don't see it listed. I encountered this problem when upgrading my project from 15.6 to 16. We didn't have any warnings so I assumed it was safe to do so. Not that it's a big deal because I was able to work around it by converting that component to use a portal, but had it been a 3rd party component this would have been a much bigger problem. Or maybe I was just misusing the lifecycle hooks in React 15.6 and it never provided those guarantees in the first place? Anyway, I'll let you guys decide. |
I agree this wasn’t called out specifically. I still don’t know if it’s a bug or not. (I didn’t have time to look into this issue yet and probably won’t have for a while.) The point I was thinking about is:
It doesn’t match your description, but I think it might have the same root cause. The internal description of this change could be “React is no longer re-entrant, so it’s not possible to begin rendering a component tree while we’re already rendering a component tree.” But that’s hard to understand. So we provided one possible effect of this. There might be others—thanks for helping us discover them. In general the recommendation is to not use nested |
@gaearon this bug is now 4 months old and still reproduces on 16.2.0, but you suggest to create another one at StackOverflow ;) Any chances to fix this? |
That StackOverflow post didn’t mention anything about a nested I will however edit my SO answer to better clarify this. I’m closing this particular issue because it is expected with React 16 that if you do If you have another issue with refs not attaching in time and don’t have nested |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
componentDidUpdate
fires beforesetRef
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
https://codesandbox.io/s/vnmw1090ly
Open the Console to see what's going on:
Pasting code here too for safe-keeping:
What is the expected behavior?
componentDidUpdate
should fire aftersetRef
.In the example above, if you open the Console, you will see
"hello" null
. Thenull
is supposed to be a reference to the<div>
but it hasn't been set yet. Why not?componentDidUpdate
is supposed to fire after the component has rendered and allref
s are called.Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.2.0, Chrome 63 on Ubuntu 16.04.
Yes, this worked fine in React 15.6
More details:
This sounds a lot like this issue reported on BountySource for react-bootstrap/react-bootstrap (@gaearon commented on this), but it doesn't look like they filed an issue with React. I did manage to produce an SSCCE, so I'm reporting it now.
The text was updated successfully, but these errors were encountered: