question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Reactions:25
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

52reactions
waleedshktcommented, Aug 14, 2021

I found a simple solution.

Wrap the child functional component with a <div>.

24reactions
oliviertassinaricommented, May 27, 2019

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.

import React from "react";
import { NavLink } from "react-router-dom";
import PropTypes from "prop-types";
import Menu from "@material-ui/core/Menu";
import MenuItem from "@material-ui/core/MenuItem";
import ListItemIcon from "@material-ui/core/ListItemIcon";
import ListItemText from "@material-ui/core/ListItemText";
import Icon from "@material-ui/core/Icon";

const ForwardNavLink = React.forwardRef((props, ref) => (
  <NavLink {...props} innerRef={ref} />
));

export default const PopupItem = ({ currentTarget, childrenMenu, closePopup }) => (
  <Menu
    anchorEl={currentTarget}
    open={!!currentTarget}
    onClose={closePopup}
  >
    {childrenMenu.map((val, index) => {
      const {
        text: textChildren,
        icon: iconChildren,
        link: linkChildren
      } = val;

      return (
        <MenuItem
          key={`MPU-${index}-${textChildren}`}
          onClick={closePopup}
          className="nested"
          style={{ color: "inherit" }}
          to={linkChildren}
          exact
          activeClassName="active-menu"
          style={{ textDecoration: "none", color: "inherit" }}
          component={ForwardNavLink}
        >
          <ListItemIcon>
            <Icon className="icon material-icons-round">{iconChildren}</Icon>
          </ListItemIcon>
          <ListItemText primary={textChildren} className="text" />
        </MenuItem>
      );
    })}
  </Menu>
);

https://codesandbox.io/s/createreactapp-x6u1h

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found