Skip to content

Dropdown menu item cannot use hooks #1349

Closed Answered by mayank99
saskliutas asked this question in Q&A
Discussion options

You must be logged in to vote

The menuItems prop is supposed to be an array of functions which return a component, rather than an array of components.

You can fix it like this:

function ContextItem({ onClick, ...props }: React.ComponentPropsWithoutRef<typeof MenuItem>) {
  const { message } = React.useContext(internalContext);
  return (
    <MenuItem
      onClick={(...args) => {
        console.log(`Context Item Clicked - ${message}`);
        onClick?.(...args);
      }}
      {...props}
    >
      ContextItem
    </MenuItem>
  );
}
- ContextItem,
+ ({ close }) => <ContextItem onClick={close} />,

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@saskliutas
Comment options

saskliutas Jun 12, 2023
Collaborator Author

Answer selected by saskliutas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants