Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
What are Action's creators in React Redux?
Next article icon

What are the 3 core concepts of React Redux ?

Last Updated : 18 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Table of Content

  • Actions
  • Reducers
  • Store

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:

Screenshot30
Project Structure

The 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:

redux-output
Output

Next Article
What are Action's creators in React Redux?
author
tapeshdua420
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • React-Redux

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 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 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 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 React Redux, middlewares are an essential concept for handling side effects and enhancing the functionality of Redux. They are used to intercept actions sent to the Redux store and modify them before they reach the reducer or after they are dispatched. Understanding ReduxBefore diving into middle
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences