-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Menu Component "Function components cannot be given refs" #15903
Comments
@Hens94 Please provide a full reproduction test case. This would help a lot 👷 . |
@oliviertassinari |
This is an issue with the I have noticed another issue; you are using the 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>
); |
Is there a way for MUI to provide a more helpful error message for this? I was baffled by jcoreio/material-ui-popup-state#11 for awhile until I dug into |
Also I'm passing |
Also on a broader scale -- I think it would be more convenient for everyone if React automatically forwards all refs to the first DOM node ( |
@jedwards1211 Pretty sure there are already plans to do automatic forwarding in React. I can’t remember where I read it though. |
@jedwards1211 Please open a new issue and fill out the template. We already have additional warnings for function components been given refs. If there are instances where this doesn't happen I'd like to add it. |
I found a simple solution. Wrap the child functional component with a |
This comment has been minimized.
This comment has been minimized.
@waleedshkt Ty for solution :-D :-D Im using MUI v4 and <Menu
keepMounted
open={contextMenu !== null}
onClose={() => handleClose(setContextMenu)}
anchorReference="anchorPosition"
anchorPosition={
(contextMenu !== null)
? { top: contextMenu.mouseY, left: contextMenu.mouseX }
: undefined
}
>
<div>
<ContextMenuItem
setState={setContextMenu}
valueText="Example"
>
<SendIcon fontSize="small" />
</ContextMenuItem>
</div>
<div>
<ContextMenuItem
setState={setContextMenu}
valueText="Delete panel"
customFunc={() => removeAllPanelDataSaga({ panelId })}
>
<DeleteForeverIcon fontSize="small" />
</ContextMenuItem>
</div>
</Menu> |
Eliminate two causes of console warnings. 1. The 'elevation' prop is a number, not a string. 2. Due to a bug referenced in the following links, put a 'div' around 'children'. See jcoreio/material-ui-popup-state#11 and mui/material-ui#15903 (comment)
This comment has been minimized.
This comment has been minimized.
I saw a similar (probably the same) issue with i.e. // "Function components cannot be given refs" error message in console
<Fade in={open}>
<MyModalComponent handleClose={handleClose} />
</Fade>
// The message is gone
<Fade in={open}>
<div>
<MyModalComponent handleClose={handleClose} />
</div>
</Fade> |
Thank you! This solved my problem. I had this breaking my application:
Changed the last part to this:
And now it works like a charm! Thank you again! |
Just like @TomasKomprs But add 1 div between function Any() {
return (
<Menu>
<div>
<AddPageLayoutBlock />
<TestBtn />
</div>
</Menu>
);
}
function Any({ children }) {
return (
<Menu>
<div>{children}</div>
</Menu>
);
} |
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 🌎
The text was updated successfully, but these errors were encountered: