Skip to content
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

Merged
merged 2 commits into from
Oct 30, 2017

Conversation

knpwrs
Copy link
Contributor

@knpwrs knpwrs commented Oct 27, 2017

The question came up this morning on our team as to if ref callbacks are guaranteed to be called before componentDidMount. 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 after componentDidMount. That issue appears to possibly be related to using an instance-bound arrow function for render. 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 before componentDidMount.

@reactjs-bot
Copy link

reactjs-bot commented Oct 27, 2017

Deploy preview ready!

Built with commit 18097a2

https://deploy-preview-216--reactjs.netlify.com

@@ -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`.
Copy link
Contributor

@bvaughn bvaughn Oct 28, 2017

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.

Copy link
Contributor

@bvaughn bvaughn Oct 28, 2017

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.

Copy link
Contributor

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 before componentDidMount or componentDidUpdate lifecycle hooks.

Copy link
Contributor Author

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.

@bvaughn bvaughn merged commit d9d6d95 into reactjs:master Oct 30, 2017
@knpwrs knpwrs deleted the ref-did-mount branch October 31, 2017 01:02
jhonmike pushed a commit to jhonmike/reactjs.org that referenced this pull request Jul 1, 2020
BetterZxx pushed a commit to BetterZxx/react.dev that referenced this pull request Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants