the Menu component doesn't accept a Fragment as a child
See original GitHub issueHi,
i’m getting this error:
Warning: Material-UI: the Menu component doesn’t accept a Fragment as a child. Consider providing an array instead.
using this code.
<Menu
open={menuOpen}
anchorEl={menuAnchorRef && menuAnchorRef.current}
onClose={handleClose}
>
{menuItems.map((item, i) => (
<React.Fragment key={item.label}>
{i !== 0 && <Divider />}
<MenuItem onClick={item.onClick}>{item.label}</MenuItem>
</React.Fragment>
))}
</Menu>
The menu seems to work just fine even when I use the fragment. You can put any and every html/react element as content for the menu so why not Fragment? Will this cause issues I cannot see right now? If it doesn’t, why even display the warning in console?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (1 by maintainers)
Top Results From Across the Web
the Menu component doesn't accept a Fragment as a child
Hi,. i'm getting this error: Warning: Material-UI: the Menu component doesn't accept a Fragment as a child. Consider providing an array instead.
Read more >Material-UI: the Select component doesn't accept a Fragment ...
js:1 Material-UI: the Select component doesn't accept a Fragment as a child. Consider providing an array instead. My code is the next: import...
Read more >Render Children in React Using Fragment or Array Components
Fragments. It used to be that React components could only return a single element. If you have ever tried to return more than...
Read more >Issue with mapping Material-UI components : r/reactjs - Reddit
... Menu component doesn't accept fragment as a child, consider passing an array instead'. I tried replacing the fragment with divs (error: ...
Read more >Material-UI: the Select component doesn't accept a Fragment ...
Coding example for the question Material-UI: the Select component doesn't accept a Fragment as a child. Consider providing an array instead-Reactjs.
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
For what it’s worth, this is how I handled this issue when trying to dynamically render native/non-native menu items. Originally wanted to extract the menu item generation to their own components to keep the JSX clean, but that wouldn’t work for non-native MenuItem.
I know it’s a bit late, but just in case someone else has the same problem: Instead of using
<React.Fragment>or<>use<div key={item.label}>Worked for me