Redux Toolkit

yarn add react-redux @reduxjs/toolkit

includes the Redux core, as well as other key packages like(Redux Thunk and Reselect, immer) and helper functions

Configure Store

  • A configureStore() function with simplified configuration options. It can automatically combine your slice reducers, adds whatever Redux middleware you supply, includes redux-thunk by default, and enables use of the Redux DevTools Extension.

const store = configureStore({
  reducer: counter
})

Create Slice

  • A createSlice() function that accepts a set of reducer functions, a slice name, and an initial state value, and automatically generates a slice reducer with corresponding action creators and action types. Default(unknown action type) returns current.

    • Mutations of state will auto create a new object on change as needed

import { createSlice } from "@reduxjs/toolkit";

const counterSlice = createSlice({
  name: "counter",
  initialState: 0,
  reducers: {
    increment: (state) => state + 1,
    decrement: (state) => state - 1,
    //called when dispatch type counter/addTodo
    addTodo(state, action) {
      const { id, text } = action.payload;
      state.push({ id, text, completed: false });
      //allowed to mutuate because function wrapped with produce from Immer library
    },
  },
});

export const { increment, decrement, addTodo } = todosSlice.actions;

export default todosSlice.reducer;

Old Helpers but unneeded b/c create Slice

  • A createReducer() utility that lets you supply a lookup table of action types to case reducer functions, rather than writing switch statements. In addition, it automatically uses the immer library to let you write simpler immutable updates with normal mutative code, like state.todos[3].completed = true.

  • A createAction() utility that returns an action creator function for the given action type string. The function itself has toString() defined, so that it can be used in place of the type constant.

  • However, we can take advantage of createAsyncThunk to clean up the above code, and to create those three actions automatically:

    What createAsyncThunk does here, is to automatically create an action creator for each Promise state. For example if we name our async thunk "users/getUsers", it generates:

    • pending: "users/getUsers/pending"

    • rejected: "users/getUsers/rejected"

    • fulfilled: "users/getUsers/fulfilled"

Last updated