VOOZH about

URL: https://www.geeksforgeeks.org/reactjs/how-to-set-initial-state-in-redux/

⇱ How to set initial state in Redux ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to set initial state in Redux ?

Last Updated : 23 Jul, 2025

Redux is the tool for the management of the state throughout the whole application globally. We can set initial states in redux as a store that can only be modified with the help of the actions. But, this state must be specified somewhere first to use it. 

Prerequisites

Approach

We prefer to declare and set initial state in Redux at the reducer files. Then we use that reducer in the store and provide that store use inside the whole application. This state in reducers will be modified only using the defined action.

Let's create an application in react with redux and set the initial state in Redux:

Steps to create React Application

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

npx create-react-app example

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

cd example

Step 3: Module installation, now from the root directory of your project in the terminal, run the following command

npm i redux react-redux

Project Structure:

👁 Image
project structure

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

{
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.1.3",
"react-scripts": "5.0.1",
"redux": "^4.2.1",
"web-vitals": "^2.1.4"
},
}

Example: Initialise two states in Reducers and manage them from React components with redux store and defined actions in the redux actions.

Step to run the application: Use the following command to run the application:

npm start

Output: There are two initial states in this example which are defined in the reducer file of redux.

  • signed: false,
  • counter: 0

which are changing by actions.

👁 Peek-2023-10-18-10-56

Comment