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:
Explain the purpose of render() in ReactJS
Next article icon

Explain the concept of Redux in React.

Last Updated : 05 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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. In this tutorial, we'll learn the concept of Redux in React.

Table of Content

  • How does redux work?
  • Steps to Create React Application And Installing Redux:
  • Advantages of Redux:

Prerequisites:

  • Node & NPM
  • React
  • JSX

How does redux work?

  • 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.

Steps to Create React Application And Installing Redux:

Step 1: Create a React Vite application and Navigate to the project directory the following command:

npm init vite@latest my-react-redux-project --template react
cd my-react-redux-project

Step 2: Install Redux and React-Redux packages

npm install redux react-redux

Project Structure:

folder
Folder 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"
}

Step 3: Create a new file named store.js in the src directory of your project.

Step 4: Create one or more reducer files inside the src Folder.

Step 5: Create a file named index.js inside the src/reducers directory to Combine reducers.

Step 6: Connect Redux to React.

  • Open src/main.js (or src/main.jsx) file.
  • Import '<Provider>' from react-redux and your Redux store.
  • Wrap your root component with '<Provider>' and pass the Redux store as a prop.

Step 7: Now, Create your React components as usual and use the connect function from react-redux to connect your components to the Redux store.

JavaScript
// src/store.js import { createStore } from 'redux'; import rootReducer from './reducers';  const store = createStore(rootReducer);  export default store; 
JavaScript
// src/reducers/counterReducer.js const initialState = {     count: 0 };  const counterReducer = (state = initialState, action) => {     switch (action.type) {         case 'INCREMENT':             return { ...state, count: state.count + 1 };         case 'DECREMENT':             return { ...state, count: state.count - 1 };         default:             return state;     } };  export default counterReducer; 
JavaScript
// src/reducers/index.js import { combineReducers } from 'redux'; import counterReducer from './counterReducer';  const rootReducer = combineReducers({   counter: counterReducer });  export default rootReducer; 
JavaScript
// src/components/Counter.jsx import React from 'react'; import { connect } from 'react-redux';  const Counter = ({ count, increment, decrement }) => {     return (         <div>             <h1 style={{ color: 'green' }}>                 GFG Counter App using Redux</h1>             <p>Count: {count}</p>             <button onClick={increment}>Increment</button>             <button onClick={decrement}>Decrement</button>         </div>     ); };  const mapStateToProps = (state) => ({     count: state.counter.count });  const mapDispatchToProps = (dispatch) => ({     increment: () => dispatch({ type: 'INCREMENT' }),     decrement: () => dispatch({ type: 'DECREMENT' }) });  export default connect(mapStateToProps,     mapDispatchToProps)(Counter); 

Start Your Application using the following command:

npm run dev

Output:

Advantages of Redux:

  • Centralized State Management: Redux maintains a single, global state tree for effective management of application state.
  • Predictable State Changes: Redux follows a clear and predictable pattern where actions trigger reducers to update the state, simplifying debugging and understanding of state changes.
  • Unidirectional Data Flow: Redux enforces a unidirectional data flow, making it easy for components to subscribe to the state and ensuring consistency in application behavior.
  • Scalability: Well-suited for larger applications, Redux provides a structured approach that scales efficiently as the complexity of the application grows.

Next Article
Explain the purpose of render() in ReactJS
author
sahilali
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • Geeks Premier League
  • React-Redux
  • Geeks Premier League 2023

Similar Reads

  • Explain the concept of generators in Redux Saga.
    Redux Saga is like a wise and powerful wizard for managing complex tasks in your React and Redux app. It specializes in handling asynchronous operations, like fetching data or dealing with side effects, making your code more organized and your app's behavior smoother. Think of it as a magical assist
    2 min read
  • Explain the concept of "lifting up in React Hooks"
    "Lifting State Up" is a concept in React that involves moving the state of a component higher up in the component tree. In React, the state is typically managed within individual components, but there are situations where multiple components need to share and synchronize the same state. When this ha
    2 min read
  • What are the 3 core concepts of React Redux ?
    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 c
    4 min read
  • Explain Selectors in React Redux
    Selectors in React Redux serve as efficient filters for accessing specific data from the Redux store. They encapsulate logic for data retrieval, optimizing performance, and promoting code reusability. By using memoization, selectors cache results to prevent unnecessary re-renders, thus enhancing ove
    2 min read
  • Explain the purpose of render() in ReactJS
    Render in React JS is a fundamental part of class components. It is used to display the component on the UI returned as HTML or JSX components. The ReactDOM.render() function takes two arguments, HTML code and an HTML element. Purpose of render()React renders HTML to the web page by using a function
    2 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
  • Explain Action’s in Redux
    In this article, we are going to learn about Action in Redux. Actions are plain JavaScript object that contains information. Action is one of the building blocks of Redux.  Redux is a state managing library used in JavaScript apps. It is used to manage the data and the state of the application. Uses
    5 min read
  • Explain new Context API in React
    Context API in React is used to share data across the components without passing the props manually through every level. It allows to create global state of data providing global access to all the components. In this article, we will discuss about the context API in React and its uses with implement
    4 min read
  • Link Component in React Router
    React Router is a powerful library in ReactJS that is used to create SPA (single-page applications) seamlessly. One of the components of the React Router is Link. In this article, we will be seeing the working of the Link component, various props, and usage of the Link component within React Router
    5 min read
  • Explain the Benefits of State Normalization in Redux
    Redux is a predictable state container for JavaScript apps and offers a powerful solution for managing application states in complex web applications. One strategy that Redux uses is state normalization in which we can restructure state data to improve performance, maintainability, and scalability.
    5 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