05 / 18

5. How do you handle pending, fulfilled, and rejected states in slices?

Handling Async Lifecycle States in Redux Toolkit Slices

In Redux Toolkit, you can handle the pending, fulfilled, and rejected states of asynchronous actions (created using createAsyncThunk) directly within your slice using the extraReducers builder syntax. This allows you to manage loading indicators, store successful responses, and handle errors consistently.

How Each State Is Handled
  1. 1

    1. pending: Update status to 'loading' to show that an async request is in progress.

  2. 2

    2. fulfilled: Update status to 'succeeded' and store the fetched or processed data from action.payload.

  3. 3

    3. rejected: Update status to 'failed' and capture the error message from action.error.message.

Example: Handling Async States in a Slice

By handling each lifecycle state within extraReducers, you can declaratively manage async data flow — ensuring your UI responds correctly to loading, success, and failure events.