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

Dealing with Button as a NavLink of react router gives a react warning #2295

Closed
sharno opened this issue Nov 6, 2017 · 8 comments
Closed

Comments

@sharno
Copy link

sharno commented Nov 6, 2017

Steps

Add a NavLink from react-router to a button, ex:
<Button as={NavLink} to={"/members/" + user.id}>Click</Button>

Expected Result

no warnings

Actual Result

Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.

Check the render method of `Button`.
    in NavLink (created by Button)
    in Button (at MembersPendingAssignments.js:35)

Version

0.75.1

Testcase

https://codesandbox.io/s/8xm89o4j3l

Is it a bug or I shouldn't use the button this way? Any alternatives to get a button working as a link that changes in styling or should I manage the styling manually and use just a Link instead?

@brianespinosa
Copy link
Member

@sharno in your codesandbox example your App component is written as a stateless functional component. I think if you write it as a class it should work.

@sharno
Copy link
Author

sharno commented Nov 7, 2017

@brianespinosa It seems not to be the case, I updated the same codesandbox to be a class component and it's still giving the same error.
The warning is obviously not pointing to the App component anyway. It's pointing to the render method of Button.

@layershifter
Copy link
Member

@sharno Your issue is correct, Button uses refs inside it and this means that it currently can't accept stateless components as as.

This will be solved finally by #1879. The simpliest workarond will be wrap NavLink to the class component or use HOC.

@sharno
Copy link
Author

sharno commented Nov 7, 2017

Thanks a lot @layershifter .. will use HOC for now until it's fixed then. I really appreciate your and other contributors' work on Semantic UI React here, it's really elegant and I use it in most of my work.

@brianespinosa
Copy link
Member

@sharno so sorry about that. I looked at your codesandbox too quickly. And thanks for catching that @layershifter.

@dknell
Copy link

dknell commented Jan 16, 2018

@layershifter @brianespinosa Was this ever fixed? I'm using 0.77.2 and still getting this error

Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.

Check the render method of `Button`.
    in NavLink (created by Button)
    in Button (created by LeftNav)
    in Unknown (created by _class)
    in _class (created by LeftNav)
    in LeftNav
    in div
    in Router (created by BrowserRouter)
    in BrowserRouter

when rendering this button:

<Button primary as={NavLink} to="/login">Login</Button>

@X-Tender
Copy link

Can someone show a HOC solution?

@brianespinosa
Copy link
Member

To update everyone who might be landing on this duplicate issue, this should be handled in #2306 which is a replacement for #1879.

@dknell this is still not resolved yet.

@Semantic-Org Semantic-Org locked as off-topic and limited conversation to collaborators Feb 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants