0

the following code works perfectly:

const onMenuItemClick = (item) => {
return {
    type: "MENU_ITEM_CLICKED"
}

this code dispatches the action twice:

const onMenuItemClick = (item) => {
document.getElementById("menu-button").click();
return {
    type: "MENU_ITEM_CLICKED"
}

my question is, why does this click makes the action dispatches twice?

Wolfgaung
  • 53
  • 5
  • 2
    That looks like it could cause an infinite loop of dispatches. – kjprice Feb 16 '17 at 20:55
  • Why would you do that? – nbkhope Feb 16 '17 at 21:15
  • i have a sidemenu that needs to close on a click at a menuitem. Normally the menu button open and closes the menu and the toggle variable is within a containers state that is not accessible by the menu items, also i cannot move the toggle to the global state. any ideas @nbkhope ? kjprice: it is exactly dispatched twice, and only this action. it doesnt seems to be a loop or a infinite loop. – Wolfgaung Feb 16 '17 at 21:29
  • 1
    If you need something else to close, you dispatch a second action that closes it. You don't programmatically trigger a click event. – Samo Feb 16 '17 at 21:42

1 Answers1

0

If anyone else has a similar problem, here is how i fixed it:

i have rewritten the application a little bit, the toggle variable is now not in the local state anymore, i have put it now on the global redux state and i can share it between multiple components.

beginner problem i guess :)

Wolfgaung
  • 53
  • 5