-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Explicitly note that ref callbacks are called before componentDidMount. #216
Conversation
Deploy preview ready! Built with commit 18097a2 |
content/docs/refs-and-the-dom.md
Outdated
@@ -66,7 +66,7 @@ class CustomTextInput extends React.Component { | |||
} | |||
``` | |||
|
|||
React will call the `ref` callback with the DOM element when the component mounts, and call it with `null` when it unmounts. | |||
React will call the `ref` callback with the DOM element when the component mounts, and call it with `null` when it unmounts. `ref` callbacks will be called before `componentDidMount`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new wording could be slightly confusing because ref
callbacks might be called after mount if a new ref
callback is provided (as is the case with arrow-functions that are recreated each time render
is run). In that case, React clears the old one and calls the new one during the render phase (before componentDidMount
/ componentDidUpdate
).
Edit: The caveat I mentioned above is mentioned later on the same docs page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guh, GitHub keeps auto-submitting my comment on Enter when I don't intend it. Sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of wording more like:
ref
callbacks are invoked beforecomponentDidMount
orcomponentDidUpdate
lifecycle hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me, I'll push an update.
Sync with reactjs.org @ b8b3db7
The question came up this morning on our team as to if
ref
callbacks are guaranteed to be called beforecomponentDidMount
. The current wording in the docs implies that the answer is "yes;" however, we did find an instance on Stack Overflow where somebody was claiming that their ref callback was called aftercomponentDidMount
. That issue appears to possibly be related to using an instance-bound arrow function forrender
. The following comment is also linked from that thread: facebook/react#6249 (comment)This PR simply makes the docs explicitly state that
ref
callbacks will be called beforecomponentDidMount
.