Redux: Is possible to dispatch array of actions at the same time in redux?

Created on 12 Jun 2017  路  1Comment  路  Source: reduxjs/redux

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.

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cloudfroster picture cloudfroster  路  3Comments

vslinko picture vslinko  路  3Comments

jbri7357 picture jbri7357  路  3Comments

vraa picture vraa  路  3Comments

elado picture elado  路  3Comments