Redux (Toolkit)

Redux Toolkit (RTK) is the "official, opinionated, batteries-included toolset for efficient Redux development".

RTK resolves many of the arguments against Redux related to boilerplate and unnecessary code.

redux-store.png

Redux Flow

![Screen Shot 2022-05-05 at 8.19.25 AM](/Users/dev/Library/Application Support/typora-user-images/Screen Shot 2022-05-05 at 8.19.25 AM.png)

The Redux Flow is made up of 3 main components:

  1. Actions –– JS objects with a required type property (among others)

    • type is simply a string that describes the action (what happened to the state) (ie. ADD_TODO).

    • They aren't responsible for changing state.

    • An action is dispatched via the store.dispatch(action) method

  2. Reducers –– pure functions that handle updating the state

    • performs the operations on it as instructed by the action

    • responsible for changing the value of the state

    • SWITCH-CASE for the action's type

  3. Store –– where all the states are managed.

    • Finally, the state will be updated in the Store

    • The components must be subscribed to the Store to listen for state updates to render the states correctly in the UI.

    • It can be created in a single line:

Step 1: Installation + Setup

You can either install RTK:

  1. along side create-react-app at a project's initial setup, OR

  2. into an existing app:

Step 2: Create + Init Store

Now create a store to hold our states.

configureStore replaces the original createStore from Redux. It not only creates a store, but it can also accept reducer functions as arguments.

Step 3: Provide Store to React Components

We need every component to have access to the Store created. This is achieved using Provider in index.js (topmost component) (much like useContext)

Step 4: Write Reducers + Actions

You can now write reducer functions (responsible for updating state based on action.type) and actions (JS objects with type-property) for our Redux Store.

Typically, you would write reducers + actions separately. But in RTK, both can be written in what's called a slice.

Slice –– collection of actions + reducers wrapped up inside the same file.

Note, in RTK:

  • SWITCH-CASES are no longer needed to manage the action with its corresponding reducer.

  • we are now directly mutating the state's value in the reducer function instead of returning a new value to update the state (thanks to Immer library)

Step 5: Import Reducer

We have exported our reducers and actions from our counterSlice.js, so let's import the reducer into our store.js:

Step 6: Add Dispatch Functions in UI

Recall from Redux Flow Diagram: our View triggers an action to be dispatched (received) in order to update a state.

With React-Redux, we can use hooks:

  • useDispatch –– to dispatch actions

  • and useSelector –– to read data from the store.

In the component that usually calls state and renders it into view, (ie. Counter.js for a Counter app), import:

  1. useDispatch + useSelector hooks

  2. actions from counterSlice.js

Then, the same Counter.js functional component will init our 2 hooks and return UI elements with our dispatch(action) triggered when clicked:

Last updated