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 } }; },
}- ✓Correct answer: Building the action payload (e.g. generating an id) before it reaches the pure reducer
- 2Running side effects after the reducer
- 3Validating the state shape
Explanation
Reducers must be pure, so random ids or timestamps are created in prepare. Callers still dispatch todoAdded("text").