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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/docs/refs-and-the-dom.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Using the `ref` callback just to set a property on the class is a common pattern for accessing DOM elements. The preferred way is to set the property in the `ref` callback like in the above example. There is even a shorter way to write it: `ref={input => this.textInput = input}`.

Expand Down