-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Document that componentDidUpdate happens before paint #8452
Comments
I was confused by this documentation too. Adding a description of a reliable way to access the rendered DOM would be much appreciated. |
The comment referenced (#2659) is really old. I wouldn't read much into it since React has changed a lot in the last 3 years.
I'm not sure I understand why this is necessary. Could you elaborate? did-mount and did-update are called after changes have been flushed to the DOM, so you can synchronously access refs or their backing DOM elements in those methods. The documentation and source code for reactjs.org now lives in a different repository: reactjs/reactjs.org. (For more info on why we made this move, see issue #11075.) Let's re-open a new issue in that repository to continue the discussion if you're still interested! |
@bvaughn The methods get called after the changes have been flushed to the DOM, but that's irrelevant. The problem is that they get called before the first browser painting happens, not after. To give you an example, when you have an element with |
Can you point me at a repro that shows this behavior, by chance?
As for the relevance 😄 I was just pointing out that |
Sorry, looks like a was hasty. In my particular case, I've nailed it down to padding causing the problem. For some reason, the 'clientWidth' that's received in cDM is smaller by the total padding amount of the surrounding component (maybe it gets painted first without the padding?). But it is accurate if obtained a bit later. I'll attach a fiddle if I can reproduce this. |
is this being caused by a Okay. Thanks. |
Nevermind, my issue was caused by something else. Sorry. |
It happens. 😄 |
I did encounter exactly the issue that |
Huh...that does not sound expected. Assuming stylesheets have already loaded (since they might affect layout) then reading |
We also use Aphrodite and I just stumbled across this:
While I haven't verified it yet, this might be the reason for the behavior I observed. |
Sounds like the likely cause to me ^ |
From the docs:
Please, correct me if I'm wrong. When the the last component in a large sequence of nested component changes, then all cDU from last child to parent are called in sequence. After the last cDU is called, then flushBatchedUpdates is run which, in turn, calls runBatchedUpdates.
Read this comment from @sebmarkbage for more context:
#2659 (comment)
If this is correct, the docs should say something like: Use this as an opportunity to enqueue DOM operations in the next execution thread.
EDIT: I guess this issue is also related to new hooks proposal: componentWasMounted and componentWasUpdated ( #7678).
In the meanwhile, it would be great if the docs specified that componentDid/Mount/Update should use requestAnimationFrame/setTimeout to access the rendered DOM.
The text was updated successfully, but these errors were encountered: