What are the 3 core concepts of React Redux ?
Last Updated :
18 Mar, 2024
Redux is a widely-used state management library that helps in managing the state in our projects. However, it comes with its own terminologies and jargon that can be confusing for beginners. Essentially, Redux comprises of three core concepts: actions, reducers, and store.
In this article, we will cover these concepts in detail and provide an example of how they can be used. By understanding these core concepts, you will be able to work with Redux more efficiently and effectively.
Working of Redux
- Redux operates by maintaining a centralized state tree, offering a method to manage the application state and address state changes.
- Actions are dispatched to the store, initiating reducers to define how the state should change.
- Reducers, being pure functions, take the previous state and an action as input and produce the new state as output.
- Components can subscribe to the store to access and update the state, ensuring a predictable uni-directional data flow throughout the application.
We will discuss about the following core concepts of Redux in this article.
Actions
Actions are nothing but a simple object of javascript, they contain the information that tells what kind of actions to perform and the payload which contains the data required to perform the action.
Syntax of Actions:
{
type: 'ACTION_TYPE',
payload: { /* data required for the action */ }
}
Functions that create these actions are known as Action Creators. These functions returns the action as an object.
function actionCreator(data) {
return {
type: 'ACTION_TYPE',
payload: data
}
}
Reducers
Reducers are pure functions of javascript that take current state and action and returns the new state. They create a new state based on the action type with the required modification and return that new state which then results in updation of the state.
Syntax of Reducers:
const reducerFunction = (state, action) => {
switch(action.type)
{
case 'ACTION_TYPE_1':
return {...state, ...action.payload};
case 'ACTION_TYPE_2':
return {...state, ...action.payload};
default:
return state;
}
}
Store
A store is a place where we store all the data, it is a single source, centralized store from where any component can update and get state.
- createStore(): To initialize store, usecreateStore() method which takes object of reducers.
- dispatch(action): To update the state, we need to dispatch an action which then triggers the reducer function to update the state.
- getState(): To get the state from the store, getState() method is used. It returns the current state of the store.
Syntax of Store:
// createStore()
const store = createStore(INITIAL_STATE);
// dispatch(action)
store.dispatch(actionCreator(data));
// getState()
const current_state = store.getState();
Steps to Create React Application And Installing Redux:
Step 1: Create a React application and Navigate to the project directory the following command:
npx create-react-app my-redux-project --template react
cd my-redux-project
Step 2: Install Redux and React-Redux packages
npm install redux react-redux
Project Structure:
Project StructureThe updated dependencies in package.json file will look like.
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^9.1.0",
"redux": "^5.0.1"
}
Example: Implementation to showcase concept of react redux.
JavaScript
// store.js
import { createStore } from 'redux';
// Define the initial state
const initialState = {
todos: []
};
// Define the reducer function
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'ADD_TODO':
return {
...state,
todos: [...state.todos, action.payload]
};
case 'REMOVE_TODO':
return {
...state,
todos: state.todos.filter(todo => todo !== action.payload)
};
default:
return state;
}
};
// Create the Redux store
const store = createStore(reducer);
// Define the action creators
export const addTodo = (todo) => {
return {
type: 'ADD_TODO',
payload: todo
};
};
export const removeTodo = (todo) => {
return {
type: 'REMOVE_TODO',
payload: todo
};
};
console.log(store.getState().todos); // todos - []
store.dispatch(addTodo('Learn about actions'));
store.dispatch(addTodo('Learn about reducers'));
store.dispatch(addTodo('Learn about stores'));
console.log(store.getState().todos); // todos - [ 3 todos ]
store.dispatch(removeTodo('Learn about actions'));
store.dispatch(removeTodo('Learn about reducers'));
console.log(store.getState().todos); // todos - [ 1 todo ]
export default store;
JavaScript
// index.js
import React from 'react';
import { createRoot } from 'react-dom/client';
import { Provider } from 'react-redux';
import store from './store';
import App from './App';
createRoot(document.getElementById('root')).render(
<Provider store={store}>
<App />
</Provider>
);
Output:
Output
Similar Reads
What are the benefits of using hooks in React-Redux?
Have you ever wondered how users create amazing websites and apps? Well, in the world of programming, they have some cool tools, and today we're going to explore one of them called "Hooks" in the superhero team of React-Redux. Prerequisites:ReactReact-ReduxReact HooksJavaScriptWhat are Hooks?Hooks a
2 min read
Explain the concept of Redux in React.
Redux is a state management library commonly used with React, although it can also be used with other JavaScript frameworks. It helps manage the state of your application. It was inspired by Flux, another state management architecture developed by Facebook for building client-side web applications.
3 min read
What are Action's creators in React Redux?
In React Redux, action creators are functions that create and return action objects. An action object is a plain JavaScript object that describes a change that should be made to the application's state. Action creators help organize and centralize the logic for creating these action objects. Action
4 min read
What is the use of React Context in React-Redux?
React Context is a feature that React provides us to manage states required in multiple components. Redux is also a state management library and solves the same problem that React Context does but in a different way. In this article, we will see in detail what is react context, why and how to use it
5 min read
What are the features of ReactJS ?
Created by Facebook, ReactJS is a JavaScript library designed for crafting dynamic and interactive applications, elevating UI/UX for web and mobile platforms. Operating as an open-source, component-based front-end library, React is dedicated to UI design and streamlines code debugging by employing a
4 min read
What are combinedReducers in React Redux?
In React Redux 'combinedReducers' is like putting together smaller pieces of a puzzle (reducers) to form one big puzzle (the root reducer). It helps manage your application state more efficiently by organizing and combining these smaller pieces, making your code cleaner and easier to maintain. Key F
3 min read
What are the advantages of using Redux with ReactJS ?
Redux is a state management tool for JavaScript applications. It is more commonly used with ReactJS but is also compatible with many other frameworks such as Angular, Vue, Preact, as well as vanilla JavaScript. It is important to note that even though React and Redux are frequently used together, th
3 min read
What are the differences between Redux and Flux in ReactJS ?
During the phase of applications or software development, we gather the requirements of customers to create a solution to solve the problem of customers or businesses. To solve problems we rely on different technologies and architecture patterns. for a long time, developers were using MVC (Model-Vie
10 min read
What are middlewares in React Redux ?
In the world of creating websites and apps, React and Redux are powerful tools used to build apps that can grow and be easily updated. React helps build user interfaces, while Redux helps keep track of the app's data in a way that makes it easy to understand. Redux uses something called "middlewares
5 min read
What is the purpose of constants in Redux ?
In Redux, we have a lot of actions and reducers defined while making an application and managing its state from the redux. Then, constants come into the picture, it provides a way to define the type of actions and reducers in one file or one place. The reasons to consider the constants: The type of
3 min read