State Management · Middle · Question 51 of 75

What is the prepare callback in a createSlice reducer for?

todoAdded: {
  reducer(state, action) { state.push(action.payload); },
  prepare(text) { return { payload: { id: nanoid(), text } }; },
}

Explanation

Reducers must be pure, so random ids or timestamps are created in prepare. Callers still dispatch todoAdded("text").

Further reading

Problem with a question

State Management · Middle · question 51

Test yourself

Take the State Management · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz