-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Update nuances of ref callback calling #8333
Conversation
For more transparency
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks! If you are contributing on behalf of someone else (eg your employer): the individual CLA is not sufficient - use https://developers.facebook.com/opensource/cla?type=company instead. Contact cla@fb.com if you have any questions. |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
Hmm, this sentence doesn't quite make sense to me. For every render, React calls the ref callback twice? Not on the first render, right? And what is the function instance exactly that this sentence refers to? |
Rough concept:
Maybe some mention of defining ref callbacks in constructor or using the public class fields proposal. |
So the right takeaway is basically, you should only use ref callbacks with the |
Thanks for the PR. Closing as the author did not respond to review, but happy to reopen if you'd like to reword it as suggested! |
I want to propose some changes to the Refs and the DOM documentation page. - Make it clear that string refs are legacy. It seems that this information got lost during the transition to new docs and only some part stayed the same, which was confusing when first reading the docs. - Clarify and explain that during render, if the ref callback is provided, it will get called twice, first with `null` and then with the rendered DOM element. Discussed in facebook#4533 and first proposed docs change in PR facebook#8333. I've also planned on adding an example for passing the refs up the component chain based on something I've needed to solve myself (e.g. you want to connect two dynamic components by line in React, so you need to both use refs and propagate them up the chain), and while it would be great to read up on this in the docs, it may be too specific for this section; I'd be happy to hear any recommendations.
I want to propose some changes to the Refs and the DOM documentation page. - Make it clear that string refs are legacy. It seems that this information got lost during the transition to new docs and only some part stayed the same, which was confusing when first reading the docs. - Clarify and explain that during render, if the ref callback is provided, it will get called twice, first with `null` and then with the rendered DOM element. Discussed in facebook#4533 and first proposed docs change in PR facebook#8333. I've also planned on adding an example for passing the refs up the component chain based on something I've needed to solve myself (e.g. you want to connect two dynamic components by line in React, so you need to both use refs and propagate them up the chain), and while it would be great to read up on this in the docs, it may be too specific for this section; I'd be happy to hear any recommendations.
If, we MUST using only this pattern, then it is redundant, because It is much easier to use the In other side, ref-function, allows write link to component with different model, e.g. in observable object. And in this case, it would be nice to know about the "extra" ref calls (for debounce save method or debounce listeners of that observable object) |
Also I think in situation where described cases when ref calls, most developers thinks that ONLY this cases exists. |
Being "easier" does not make it right, |
Hmm.. I thought it was just shortcut for |
There are multiple problems with it:
|
* Update refs-and-the-dom.md I want to propose some changes to the Refs and the DOM documentation page. - Make it clear that string refs are legacy. It seems that this information got lost during the transition to new docs and only some part stayed the same, which was confusing when first reading the docs. - Clarify and explain that during render, if the ref callback is provided, it will get called twice, first with `null` and then with the rendered DOM element. Discussed in #4533 and first proposed docs change in PR #8333. I've also planned on adding an example for passing the refs up the component chain based on something I've needed to solve myself (e.g. you want to connect two dynamic components by line in React, so you need to both use refs and propagate them up the chain), and while it would be great to read up on this in the docs, it may be too specific for this section; I'd be happy to hear any recommendations. * Adds more specific information about the callback * Moved the ref callback description to the Caveats section * Fixed suggested nits * Replace 'each render pass' with 'updates' * Tweak the wording
* Update refs-and-the-dom.md I want to propose some changes to the Refs and the DOM documentation page. - Make it clear that string refs are legacy. It seems that this information got lost during the transition to new docs and only some part stayed the same, which was confusing when first reading the docs. - Clarify and explain that during render, if the ref callback is provided, it will get called twice, first with `null` and then with the rendered DOM element. Discussed in #4533 and first proposed docs change in PR #8333. I've also planned on adding an example for passing the refs up the component chain based on something I've needed to solve myself (e.g. you want to connect two dynamic components by line in React, so you need to both use refs and propagate them up the chain), and while it would be great to read up on this in the docs, it may be too specific for this section; I'd be happy to hear any recommendations. * Adds more specific information about the callback * Moved the ref callback description to the Caveats section * Fixed suggested nits * Replace 'each render pass' with 'updates' * Tweak the wording (cherry picked from commit 4a7e06b)
@gaearon Could you please elaborate more, what's the ref declaration and use in your example? |
class MyComponent extends Component {
renderRow = (index) => {
// This won't work. Ref will get attached to DataTable rather than MyComponent:
return <input ref={'input-' + index} />;
// This would work though! Callback refs are awesome.
return <input ref={input => this['input-' + index] = input} />;
}
render() {
return <DataTable data={this.props.data} renderRow={this.renderRow} />
}
} |
@gaearon Is there any reason to avoid callback refs like The current documentation seems to suggest |
@aij I guess because |
|
@VladislavBaranov String refs will be deprecated in 16.4. 16.3 will be released with new createRef api, so I don't recommend to rely on string refs anymore. |
Why not keep string refs as a syntatic sugar for String refs are far more readable than callback refs and react could use more readability oriented features. |
I hope I'm not the only one who disagree with the reasons to remove string refs. They are a good feature, if anyone knows the reasons in more depth please comment on this issue I've just created: #15559 |
Couldn't you just:
(New to React and still learning so sorry if I'm ignorant of something here) |
This replaces usage of `this.refs` with the `createRef` API as recommneded here: https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs. I haven't meansured it yet, but this might also help improve modal performance. See: facebook/react#8333 (comment)
wewe |
For more transparency