![]() |
VOOZH | about |
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.
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:
Step 1: Create a React application using the following command:
npx create-react-app exampleStep 2: After creating your project folder i.e. example, move to it using the following command:
cd exampleStep 3: Module installation, now from the root directory of your project in the terminal, run the following command
npm i redux react-reduxThe 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 startOutput: There are two initial states in this example which are defined in the reducer file of redux.
which are changing by actions.