Reputation: 43
Stripped down code:
const menu = (
<Menu>
<Menu.Item>
1st menu item
</Menu.Item>
</Menu>
);
export const QBDropdown: React.FC = () =>
<div>
<Dropdown overlay={menu} />
</div>
When attempting to populate the overlay property in the antd dropdown, the following error always occurs:
Error: React.Children.only expected to receive a single React element child.
It does so seemingly regardless of what I populate it with. A single <div></div>
or <></>
produces the same error. What's going on here?
Upvotes: 4
Views: 12746
Reputation: 51
Try using a Dropdown like this in antd v5
const profileItems = [
{
key: "1",
label: (
<a target="_blank" rel="noopener noreferrer">
1st menu item
</a>
),
},
{
key: "2",
label: (
<a target="_blank" rel="noopener noreferrer">
2nd menu item (disabled)
</a>
),
icon: <LoginOutlined />,
disabled: true,
},
{
key: "3",
label: (
<a target="_blank" rel="noopener noreferrer">
3rd menu item (disabled)
</a>
),
disabled: true,
},
{
key: "4",
danger: true,
label: "a danger item",
}]
<Dropdown menu={{ items: profileItems }}>
<div className="logo">
<LoginOutlined />
<div>Profile</div>
</div>
</Dropdown>
Upvotes: 4
Reputation: 1121
If you face that issue after updating to antd v5
, it might be related to the new flow for the Dropdown menu
(overlay
old way).
Console warning:
Warning: [antd: Menu]
children
will be removed in next major version. Please useitems
instead.
deprecated way:
const menu = (
<Menu>
<Menu.Item key='1' onClick={() => handleDuplicate(record.id)}>
Duplicate
</Menu.Item>
</Menu>
);
<Dropdown.Button href={url} overlay={menu}>
Edit
</Dropdown.Button>
new:
const items = [{
label: 'Duplicate',
key: '1',
onClick: () => handleDuplicate(record.id),
}];
const menuProps = {
items,
};
<Dropdown.Button href={url} menu={menuProps}>
Edit
</Dropdown.Button>
Upvotes: 1
Reputation: 76
The Dropdown menu must have a child. which can be verified using React.Children.only
<div>
<Dropdown overlay={menu}>
<button>Something to trigger the menu</button>
</Dropdown>
</div>
https://reactjs.org/docs/react-api.html#reactchildrenonly
Upvotes: 6
Reputation: 298
it means ur Dropdown component must have one child
import { Menu, Dropdown, Button } from "antd";
const menu = (
<Menu>
<Menu.Item>
1st menu item
</Menu.Item>
</Menu>
);
export const QBDropdown: React.FC = () =>
<div>
<Dropdown overlay={menu} placement="dropDown">
<Button>dropdown</Button>
</Dropdown>
</div>
Upvotes: 0