Facebook Pixel

How to Set Up Redux Toolkit in a React App From Scratch

Setting up Redux Toolkit is straightforward. Here is a step-by-step guide to get a store running in a React app.

How to Set Up Redux Toolkit in a React App From Scratch

Setting up Redux Toolkit in a React app is straightforward. Here is a step-by-step guide.

Step 1: Install the Packages

Install @reduxjs/toolkit and react-redux. These two packages give you the store utilities and the React bindings.

Step 2: Create a Slice

Use createSlice to define a feature's state and reducers in one place. It auto-generates action creators for you. A cart slice might have items and add and remove reducers.

Step 3: Configure the Store

Use configureStore to create the store, combining your slices under their keys. configureStore sets up sensible defaults like DevTools and middleware for you.

Step 4: Provide the Store

Wrap your app in a Provider from react-redux, passing the store. This makes the store available to all components.

Step 5: Read State With useSelector

In a component, use the useSelector hook to read the part of state you need. Select only the slice you need to avoid unnecessary re-renders.

Step 6: Dispatch Actions With useDispatch

Use the useDispatch hook to get the dispatch function, and call it with action creators from your slice to update state.

Step 7: Handle Async With createAsyncThunk

For async logic like fetching data, use createAsyncThunk, and handle its pending, fulfilled, and rejected cases in extraReducers.

The Takeaway

Install the packages, create slices with createSlice, configure the store, provide it, read state with useSelector, dispatch actions with useDispatch, and handle async with createAsyncThunk. That is the full RTK setup.

Install @reduxjs/toolkit and react-redux, create a slice with createSlice, configure the store with configureStore, wrap your app in a Provider, read state with useSelector, dispatch actions with useDispatch, and handle async with createAsyncThunk.

@reduxjs/toolkit for the store utilities like createSlice and configureStore, and react-redux for the React bindings like Provider, useSelector, and useDispatch. Both are needed.

Use createSlice, defining the initial state and reducers in one place. It auto-generates action creators for you. A cart slice might have an items array and add and remove reducers.

Use the useSelector hook from react-redux, selecting only the part of state you need. Selecting only the slice you need avoids unnecessary re-renders when other parts of state change.

Use the useDispatch hook to get the dispatch function, and call it with action creators from your slice to update state. For async logic, use createAsyncThunk and dispatch the thunk.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.