JS Built-ins
redux-toolkitcreateSliceintermediate

Reset Action in a Slice

πŸ“– Usage Example
reducers: {
  reset: () => ({ value: 0, step: 1 }),
}
// Returning a value from an Immer reducer replaces state entirely.

Returning a plain object from an Immer reducer replaces the entire state β€” perfect for reset actions.

Reset Action in a Slice

A reset reducer that returns the initial state is a common pattern. You can return a plain object from a reducer β€” Immer will use the returned value instead of the draft.

Challenge: Implement a counter slice with increment, setStep, and reset reducers.

// After setStep(5) and increment:
state.value // β†’ 5
// After reset:
state        // β†’ { value: 0, step: 1 }
πŸ’‘ Hints (3)
  • β€’ Return a brand-new object from the reducer to replace the entire state: `reset: () => ({ value: 0, step: 1 })`.
  • β€’ When you return a value from an Immer reducer, the returned value replaces the draft.
  • β€’ `state.value += state.step` reads the current step before adding.