乐闻世界logo
搜索文章和话题

Where to dispatch multiple actions in redux

2个答案

1
2

When you want to dispatch multiple actions simultaneously, Redux itself does not provide a direct method for doing so because each dispatch call typically handles only one action. However, several patterns can achieve similar effects:

1. Sequential Dispatch

The simplest approach is to call dispatch multiple times sequentially, with each call dispatching one action.

javascript
dispatch(actionCreator1()); dispatch(actionCreator2()); dispatch(actionCreator3());

This method may cause multiple re-renders if each action changes the Redux state.

2. Batch Dispatch (Middleware)

You can use middleware to extend Redux's functionality, such as redux-batch, which bundles multiple actions into a single batch action that the middleware then expands and dispatches sequentially.

javascript
import { batchDispatchMiddleware } from 'redux-batch'; // Apply middleware const store = createStore( reducer, applyMiddleware(batchDispatchMiddleware) ); // Dispatch batch actions store.dispatch(batchActions([actionCreator1(), actionCreator2(), actionCreator3()]));

This approach reduces unnecessary re-renders because state updates occur only after all actions are processed.

3. Dispatch in Promise

For asynchronous actions, you can chain them within a Promise using Promise.all or async/await. However, this only applies to asynchronous actions and processes them sequentially, not truly simultaneously.

javascript
Promise.all([ dispatch(asyncActionCreator1()), dispatch(asyncActionCreator2()), dispatch(asyncActionCreator3()), ]);

Or using async/await:

javascript
async function dispatchMultipleActions() { await dispatch(asyncActionCreator1()); await dispatch(asyncActionCreator2()); await dispatch(asyncActionCreator3()); }

4. Custom Action Creators

You can create an action creator that returns a function (thunk) rather than an action object, which dispatches multiple actions.

javascript
const compositeAction = () => (dispatch, getState) => { dispatch(actionCreator1()); dispatch(actionCreator2()); dispatch(actionCreator3()); }; // Then use dispatch(compositeAction());

This approach is typically used with the redux-thunk middleware.

In practical applications, sequential dispatch is the simplest and most direct method. However, to avoid multiple re-renders, batch dispatch or encapsulating actions is more effective. It's important to evaluate your application's performance requirements and the complexity of state updates to choose the most suitable approach.

2024年6月29日 12:07 回复

According to the recommended method in the documentation here, you can implement it within the action creator as follows:

javascript
function actionCreator(payload) { return dispatch => { dispatch(action1(payload)) dispatch(action2(payload)) } }

Then, you might want to attach the action creator as a prop and use the example provided in mapDispatchToProps here. So it looks like this:

javascript
const mapDispatchToProps = dispatch => ({ action1: some_payload => dispatch(action1(some_payload)) action2: some_payload => dispatch(action2(some_payload)) }) // your component export default connect(mapStateToProps, mapDispatchToProps)(YourApp)

2024年6月29日 12:07 回复

你的答案