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

Confusion around obtaining a ref to a Button/IconButton element #10081

Closed
gnapse opened this issue Jan 29, 2018 · 7 comments
Closed

Confusion around obtaining a ref to a Button/IconButton element #10081

gnapse opened this issue Jan 29, 2018 · 7 comments
Assignees
Labels
component: ButtonBase The React component.

Comments

@gnapse
Copy link
Contributor

gnapse commented Jan 29, 2018

When reading the documentation, I find it confusing that IconButton (doc) is documented to have a buttonRef and a rootRef props. What's the difference between those two? Also, why Button (doc) is documented to have none of those? And then ButtonBase (doc) has rootRef but no buttonRef.

Additionally, I find that in the release notes of v1.0.0-beta.27 rootRef is being removed from some components (although I don't see any button component being mentioned there).

Finally, while using these components under the latest release (beta.30) which is already after beta.27, I get buttons to work using rootRef, even when they don't have this prop documented. And I get icon buttons to work with buttonRef but not with rootRef, even when they have both documented.

So which one is it?

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 29, 2018

I have removed the ref properties form most of the components: #10025. It will be released in beta.31 this evening.
The documentation is implicit with the spread. For most use cases, you should use none of them. Just the ref.

@gnapse
Copy link
Contributor Author

gnapse commented Jan 29, 2018

But the ref on a Button or IconButton does not give me the actual button dom element, which is what I need, and what I obtain via rootRef. What can I do to obtain the native button html element reference then?

@gnapse
Copy link
Contributor Author

gnapse commented Jan 29, 2018

I see the linked recommendation, but how can you recommend switching over to use that, when the use of findDOMNode is being discouraged, and that api could be deprecated by React in the future? (see here)

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 29, 2018

when the use of findDOMNode is being discouraged, and that api could be deprecated by React in the future?

This documentation section was added with #9768. Another React issue to have a look at facebook/react#11401. It's suggesting the addition of a special "hostRef" prop that only works on host components (<div>, etc.). It would solve your problem. They are also discussing the deprecation of ref completely.

What can I do to obtain the native button html element reference then?

We can add a buttonRef property to the ButtonBase component in this case. Simple and scalable solution 👍. I have kept the inputRef property, It work nicely.
If you are uncomfortable with using findDOMNode directly, you can always abstract it.

@oliviertassinari oliviertassinari added the component: ButtonBase The React component. label Jan 29, 2018
@gnapse
Copy link
Contributor Author

gnapse commented Jan 29, 2018

Ok, good to know about that other react issue. BTW, that one also does not recommend the use of findDOMNode. So I'm not sure I understand the direction this project is taking in officially recommending its use. Presumably is until React makes its mind about this hostRef thing. I'd still prefer to avoid the currently recommended solution to root refs since definitely all proposals seem to discourage the use findDOMNode.

I like the buttonRef support for both Button and IconButton. Looking forward to it. Thanks!

@oliviertassinari
Copy link
Member

I like the buttonRef support for both Button and IconButton. Looking forward to it. Thanks!

Alright. Let's implement it :).

@chandhya
Copy link

Thanks for discussion thread on the issue
Read through the entire issue, but couldn't find the resolution I'm seeking!
@oliviertassinari , Using buttonRef property with IconButton, only returns null.
Is there a different property for using 'ref' related functionality? Can I create a codepen with an example, and share it here?

Would really appreciate a response, with any possible updates that have been made :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: ButtonBase The React component.
Projects
None yet
Development

No branches or pull requests

3 participants