How to transform an action creator with multiple dispatches in redux toolkit?

Viewed 410

I'm attempting to move my redux store towards the redux toolkit. Favourably I'd like to put them all into createSlice.

Before I had many of those kind of dispatches:

export const resetSelection = () => (dispatch, getState) => {
  dispatch(clearSelection()); # clears current selection
  dispatch(showLoader()); # shows global loading spinner
  dispatch(fetchSelection()); # fetching selection with no filters applied
};

NOTE: showLoader is part of a different reducer.

Or:

export const loadDataTypes = () => async (dispatch) => {
  dispatch(loadingDataTypes());
  const data = await genericJsonFetch(GET_DATA_TYPES);
  dispatch(setDataTypes(data));
  dispatch(loadingDataTypes());
};

How would I do this with the redux toolkit, as I can't trigger a dispatch within a reducer obviously?

1 Answers

That code will be working exactly as before and is perfectly fine to continue using with redux toolkit.

Redux toolkit gives you some useful abstractions, but you should not only use the tools redux toolkit gives you.

Please note though that the official redux style guide suggests that you allow many reducers to respond to the same action. So you should most of the time just dispatch one action and react to it in three different slices using extraReducers.

Related