How to Build Redux Store and Manage Complex State in ReactJS

After installing the required modules.

  1. First, create a store using the configureStore method provided by the redux toolkit inside the store.js file. This will be your store but we haven’t created reducers.
  2. Now wrap up the whole application using provider which provide the store we created to the application.
  3. Now create slices for the store, use createSlice method from toolkit to create a slice which contains..
    • Name of slice
    • Initial states
    • Reducer which then contains action
  4. Export the reducer and actions
  5. Imort redcuer inside store to register it.
  6. Now its time to use it, select the state inside component using hook and import corresponding actions.
  7. When update required, dispatch the action, this will update the state in store and inside component.

Steps to Create a React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app redux_store

Step 2: After creating your project folder i.e. project_name, move to the folder using the following command:

cd redux_store

Step 3: Install React Redux Module:

npm install @reduxjs/toolkit react-redux

Project Structure:

project structure

The updated dependencies in package.json file will look like.

"dependencies": {
"@reduxjs/toolkit": "^2.2.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^9.1.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

How to Create Store in React Redux ?

React Redux is a JavaScript library that is used to create and maintain state in React Applications efficiently.

Here React Redux solves the problem by creating a redux store that stores the state and provides methods to use the state inside any component directly or to manipulate the state in a defined manner.

Table of Content

  • Redux Toolkit
  • How to Build Redux Store and Manage Complex State in ReactJS
  • Wrap App with Redux Provider
  • Create Redux Store
  • Create Redux State Slice Reducer
  • Register State Slice in Store
  • Use Redux State in React Component

Similar Reads

Redux Toolkit

The Redux toolkit acts as a wrapper around Redux and encapsulates its necessary functions. Redux toolkit is flexible and provides a simple way to make a store for large applications. It follows the SOPE principle which means it is Simple, Opinionated, Powerful, and Effective....

How to Build Redux Store and Manage Complex State in ReactJS

After installing the required modules....

Wrap App with Redux Provider

Inside index.js import store from store.js and provider from react redux...

Create Redux Store

Now create a folder store.js and create a store inside it.Also export it, currently it does not include any reducer, we will register the reducers once they created....

Create Redux State Slice Reducer

Now create a slice, this include reducer and initial state. Also name should be unique. To create slice create another file slice.js.After that export slice reducer and actions...

Register State Slice in Store

Just import the slice reducer inside store and register it inside the store....

Use Redux State in React Component

Now select the state inside your component using hook:...

Contact Us