the Menu component doesn't accept a Fragment as a child

See original GitHub issue

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.

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

github_iconTop GitHub Comments

40reactions
abohannoncommented, Jun 9, 2021

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.

<Select
        className={classnames(classes.select, selectClassName)}
        onChange={onChange}
        value={selectedTopic}
        autoWidth
        native={isWidthDown('xs', width) ? true : false}
        input={
          <FilledInput
            name="topic"
            id="topic-select-input"
            className={classes.filledInput}
            disableUnderline
          />
        }
        MenuProps={{
          style: {
            zIndex: 1500,
          },
        }}
      >
        {isMobile
          ? [
              <option value="" />,
              topicData.map(topic => (
                <option
                  key={`${topic.text}-LearnNav-menuItem`}
                  value={topic.path}
                >
                  {topic.text}
                </option>
              )),
            ]
          : [
              <MenuItem value="">
                <em>None</em>
              </MenuItem>,
              topicData.map(topic => (
                <MenuItem
                  key={`${topic.text}-LearnNav-menuItem`}
                  value={topic.path}
                >
                  {topic.text}
                </MenuItem>
              )),
            ]}
      </Select>
13reactions
rbagrincommented, Nov 19, 2021

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

Read more comments on GitHub >

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

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