Is it possible to dispatch multiple actions on a redux store?
I'm trying to dispatch an action_2, while dispatching action_1. Is there an elegant way to do the same?
[I want to dispatch action2 immediately after dispatching action1 (data dependency).]
You could dispatch them at the same time in a batch (would require custom middleware to then submit each action in the batch)
If the second action is a result of the first then you could
use Redux-Saga to dispatch the second after it sees the first
use Redux-Loop to dispatch from within the store (not recommended!)
use Redux-Thunk to dispatch the first and then inspect the new state and dispatch the second
You could also look at ways to refactor your flow so that action 2 isn't dependent upon action 1. You didn't give enough details to know if this is possible. I wrote a little about this scenario here: Redux Hero Part 3. The example is a game where you've got an action to increase player XP, which could trigger an action to level up the player.
redux-batched-actions is a batching strategy using higher order reducers that seems to have some traction. It avoid multiple notifications to subscribers so it will not trigger multiple-rerenders, unlike the redux-thunk based approach suggested in #3.
I did try redux-batched actions. that's what i'm looking for. But unfortunately my action_2 happens with in a map function, and my entire store disappears.
Philip Davis
Software developer
There are lots of ways to handle this.
If the second action is a result of the first then you could
You could also look at ways to refactor your flow so that action 2 isn't dependent upon action 1. You didn't give enough details to know if this is possible. I wrote a little about this scenario here: Redux Hero Part 3. The example is a game where you've got an action to increase player XP, which could trigger an action to level up the player.