'Redux toolkit can I pass several payloads to the action?

Before in Redux, I can pass payloads like:

export const quizSetState = (answerState, results) => {
return {
    type: QUIZ_SET_STATE,
    answerState,
    results,
  };
};
case QUIZ_SET_STATE:
 return {
  ...state,
  answerState: action.answerState,
  results: action.results,
 };

But how to do it in Redux-toolkit

dispatch(quizSetState({ [answerId]: 'success' }, results));
   
quizSetState: (state, action) => {
  state.answerState = action.answerState;
  state.results = action.results;
}

I can't handle this and instead dispatching 2 times

quizSetAnswerState: (state, action) => {
   state.answerState = action.payload;
},
quizSetResultsState: (state, action) => {
   state.results = action.payload;
},

dispatch(quizSetAnswerState({ [answerId]: 'success' }));
dispatch(quizSetResultsState(results));

PS: I read docs and there were preapre callback



Solution 1:[1]

action.payload can be an object that contains the variables you want to set, you can try this:

quizSetState: (state, action) => {
  state.answerState = action.payload.answerState;
  state.results = action.payload.results;
}

dispatch(quizSetState({answerState: { [answerId]: 'success' }, results})

Hope this helps!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dlarroder