question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MenuOptionGroup onChange does not work on new menu items added dynamically

See original GitHub issue

🐛 Bug report

Re-renders won’t update onChange because of memoization here: https://github.com/chakra-ui/chakra-ui/blob/a505441cafa1f1a5593eeae395feadd8083aae14/packages/menu/src/use-menu.ts#L572 .

💥 Steps to reproduce

          <MenuOptionGroup
            type="radio"
            onChange={(ids) => {
              const selected = items.filter((deck) => ids.includes(deck.id))>
              {items.map((item) => (
                <MenuItemOption key={item.id} value={item.id}>
                  {item.name}
                </MenuItemOption>
              ))}
  1. add a new item to items array dynamically by setting state in parent
  2. click on item in menu
  3. selected is undefined

🧐 Expected behavior

New item should be selectable.

🧭 Possible Solution

Don’t useCallback in https://github.com/chakra-ui/chakra-ui/blob/a505441cafa1f1a5593eeae395feadd8083aae14/packages/menu/src/use-menu.ts#L572 .

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
mcbrohcommented, Apr 26, 2021

You can solve this by using the MenuItemOption, use the onClick props together with the isCheckedprops and this should give you the same behavior you are looking for. I hope this helps

0reactions
natBizitzacommented, Oct 13, 2022

Any news on this one?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically created select menu on change not working
I'm then trying to add an on 'change' event handler to this added select menu. The on 'change' event doesn't work on thw...
Read more >
Menu - Chakra UI
MenuOptionGroup : A wrapper for checkable menu items (radio and checkbox). ... {/* MenuItems are not rendered unless Menu is open */}. <MenuItem>New...
Read more >
ReactJS Evergreen Menu Component - GeeksforGeeks
onChange : It is a callback function that is called when selection changes. options: It is used to denote the list of options...
Read more >
How to Change menu items dynamically in React ... - Syncfusion
The items visible in the ContextMenu can be changed dynamically based on the context you open. To achieve this behavior, initialize ContextMenu with...
Read more >
How to make a dynamic onChange handler to <NumberInput ...
This behavior doesn't allow for an onChange handler that can be applied to ... value without having to create a new state or...
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