Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is possible to dispatch array of actions at the same time in redux? #2449

Closed
Vasikaran opened this issue Jun 12, 2017 · 1 comment
Closed

Comments

@Vasikaran
Copy link

My requirement is dispatch array of actions at the same time. I am doing this by using Higher Order Store.
I wrapped dispatch method and resolved it. Now my code is,

function dispatch(actions) {
if (typeof actions.type === 'undefined') {
throw new Error(
'actions may not have an undefined "type" property. ' +
'Have you misspelled a constant?'
)
}

if (isDispatching) {
  throw new Error('Reducers may not dispatch actions.')
}

try {
  isDispatching = true
  if (Array.isArray(actions)){
      actions.forEach(action=>{
          currentState = currentReducer(currentState, action);
      })
  }else if (typeof actions === 'object'){
      currentState = currentReducer(currentState, actions);
  }
} finally {
  isDispatching = false
}
const listeners = currentListeners = nextListeners
for (let i = 0; i < listeners.length; i++) {
  const listener = listeners[i]
  listener()
}
return actions;

}

If you have added this feature to Redux's dispatch method helpful for us.

@markerikson
Copy link
Contributor

See the very long prior discussion in #1813 for more info on this.

I recommend using the redux-batch store enhancer at https://github.com/manaflair/redux-batch if you're looking to dispatch an array of actions with only a single subscriber notification. There's also a variety of other approaches for batching (higher-order reducers, middleware, etc). See the Store#Batching and Notifications section of my Redux addons catalog for links to other batching-related libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants