JS Built-ins
redux-toolkitcreateAsyncThunkintermediate

Handling Pending State

πŸ“– Usage Example
extraReducers: builder => {
  builder
    .addCase(fetchData.pending, state => { state.loading = true })
    .addCase(fetchData.fulfilled, (state, action) => {
      state.loading = false
      state.data = action.payload
    })
}

The pending action is dispatched synchronously before the async work starts β€” ideal for showing a loading spinner.

Handling Pending State

The pending action fires immediately when a thunk is dispatched. Use it to show a loading indicator.

Challenge: Add a loading state that becomes true on pending and false on fulfilled.

dataSlice.reducer(undefined, fetchData.pending('req'))
// β†’ { loading: true, value: null }
πŸ’‘ Hints (3)
  • β€’ Use `fetchData.pending` as the action creator in `builder.addCase`.
  • β€’ The pending handler has no payload β€” just set `state.loading = true`.
  • β€’ The fulfilled payload is `action.payload` β€” here it's `{ value: 42 }`.