-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Remove or update use of refs #405
Comments
Can you explain the downside of using ref? At least the syntax looks quite clean to me. |
Facebook will likely deprecate
However, this issue should actually be updated. As Dan Abramov noted in eslint-plugin-react, In general, refs should be avoided. React is a virtual DOM environment, when using a ref, you break out of that virtual DOM into the real DOM. It makes testing more difficult and opens the door for issues. I'd like to remove as many use of refs as possible, and those that are 100% necessary should be callbacks. |
More context, Finally, I'd like to share this point of view. When accessing a ref, you are accessing a regular DOM node. You've left React and the Virtual DOM. Odds are, you are doing so to manage focus or measure the size/position of the node in the DOM. That is because React does not yet have a model for focus nor size/position (though Sebastian Markbåge says they are thinking of ways to handle focus). I'm starting to think it makes more sense to just use
So, what justification is there to use React refs to locate the node at all for focus / measuring purposes? |
Just checked and the limited refs we must have are now callback form. |
What about the use case used as the example on React documentation? https://facebook.github.io/react/docs/refs-and-the-dom.html You could use the name of the input instead of the reference but seems legit to me. |
It will not work for a stateless functional component (no refs) nor will it work for a composite component since the ref is a reference to the instance of the class itself, not a DOM node. From the outside, as a consumer of a 3rd party component, there is no mechanism for telling the difference between these cases nor a mechanism for dealing with the differences. |
I see your point now. So, if you want to focus on a specific element after page load, you would do something like this?
? Thanks |
Well, componentDidMount() {
const queryInput = document.querySelector('input["name=query"]')
if (queryInput) queryInput.focus();
} I should note, Dan Abramov still suggests the use of jsx-eslint/eslint-plugin-react#678 All of the points I've raised here are raised in great detail by others there ^. They also give code examples and even example repos of these issues. |
Great. Thanks for your notes. |
See update below:
Some components use aref
where they can likely usefindDOMNode(this)
instead. Regarding nested components, we have reliable markup and classNames to use. We should preferfindDOMNode(this).querySelector('...')
to find nodes nested within ourselves. We should be able to remove most, if not all, use of refs in this way.The text was updated successfully, but these errors were encountered: