Menu Component "Function components cannot be given refs"
See original GitHub issue- This is not a v0.x issue.
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior 🤔
When trying to set the anchorEl to a Menu component, I expect it to work fine without warning
Current Behavior 😯
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of ForwardRef(Menu)
.
Steps to Reproduce 🕹
Link: https://codesandbox.io/embed/createreactapp-bnnxl the warning appear when click it the Home Icon
Your Environment 🌎
Tech | Version |
---|---|
Material-UI | v4.0.1 |
React | 16.8.6 |
Browser | Chrome v74 |
Issue Analytics
- State:
- Created 4 years ago
- Reactions:25
- Comments:15 (7 by maintainers)
Top Results From Across the Web
React Warning: Function components cannot be given refs
The StyledMenu component tries to assign a ref to your ProjectMenuItem components. Refs cannot be used on function components because they ...
Read more >Menu Component "Function components cannot be given refs"
This is an issue with the NavLink component. This react-router component should forward its ref, it doesn't yet: remix-run/react-router#6056 ( ...
Read more >ref prop should not be used on React function components
This rule applies when a ref prop is specified on a React function component. The ref prop is used to provide a reference...
Read more >next link function components cannot be given refs. attempts ...
Wrapping the functional component with react.forwardRef() is a workaround, but ideally this would be fixed upstream from consumers of this library. Open side ......
Read more >Composition - Material UI - MUI
Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?. Note that you will...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I found a simple solution.
Wrap the child functional component with a
<div>
.This is an issue with the
NavLink
component. This react-router component should forward its ref, it doesn’t yet: https://github.com/ReactTraining/react-router/issues/6056#issuecomment-435524678. I hope https://material-ui.com/guides/composition/#react-router-demo helps to explain why in more details.I have noticed another issue; you are using the
ul > a > li
DOM structure. This is not encouraged. You should consider merging the two.https://codesandbox.io/s/createreactapp-x6u1h