14 / 14

How do you reset a slice’s state to its initial value?

In Redux Toolkit, you can reset a slice’s state back to its initial value by defining a reset action inside the slice or by handling an external reset action using extraReducers.

Ways to Reset Slice State
  1. 1

    Define a reducer (e.g., resetState) that returns the initialState directly.

  2. 2

    Use an external shared action with extraReducers to reset multiple slices simultaneously.

  3. 3

    Returning the initialState creates a fresh copy of the default state, ensuring immutability.

Example 1: Reset Using Reducer Inside Slice
Example 2: Reset Using Shared Action Across Slices