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:
Getting Started with React
Next article icon

Getting Started with Redux

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Redux is a popular state management library for JavaScript applications. It provides a way to centralize the state of an application in a single store, making it easier to debug, test, and reason about the state changes in the application. It helps to manage complex application states, making it easier to handle data flow and interactions.

In this article, we'll go over the basics of Redux and explore how it simplifies state management.

The following fundamental concepts are discussed in this article:

Table of Content

  • Redux
  • Steps to simplify State Management using Redux
  • Store Creation
  • Action Creation
  • Dispatching Actions
  • Reducer Functions
  • Combining Reducers
  • Connecting to React Component

What is Redux?

Redux is a state managing library used in JavaScript apps. It simply manages the state of your application or in other words, it is used to manage the data of the application. It is used with a library like React.

Setting Up Redux in a React App

  1. Store Creation
  2. Action Creation
  3. Dispatching Actions
  4. Reducer Functions
  5. Combining Reducers
  6. Connecting to React Component

1. Store Creation in Redux

To build a Redux store, developers use the redux library's createStore function and send in a root reducer as an argument. A root reducer is a collection of reducers that describe how the state in an application changes. Here's an illustration of a store built in Redux:

JavaScript
import { createStore } from 'redux';   import rootReducer from './reducers';  // Create the redux store by calling createStore  // and passing in the root reducer const store = createStore(rootReducer); 

2. Action Creation in Redux

Redux actions are simple objects that explain changes to the state. Developers define an object with a type property and any other data required to describe the change to make an action. Here's an illustration of how to make an action in Redux:

JavaScript
// Define a action creator function that  // takes test as an argument and returns // an action object.  const addTodo = (text) => {     return {          // Describes the action to be taken         type: 'ADD_TODO',         text     }; }; 

3. Dispatching Actions in Redux

To dispatch an action and update the state, developers call the dispatch method on the store and pass in the action as an argument. Here is an example of dispatching an action in Redux:

JavaScript
// Dispatch the addTodo action by calling  // store.dispatch and passing in the action store.dispatch(addTodo('Learn Redux')); 

4. Reducer Functions in Redux

Redux reducers are pure functions in Redux that accept the current state and an action and return the next state. Here's an example of a Redux reducer function:

JavaScript
// Define a reducer function that accepts the // current state and an action and return the // next state  const todoReducer = (state = [], action) => {      // To handle different action types     switch (action.type) {          // For the ADD_TODO action type         case 'ADD_TODO':             return [                  // Create a new array with the                  // existing todos                 ...state,                 {                     // Add a new todo with the text                     // from the action                     text: action.text,                      // Set the completed property                     // to false                     completed: false                 }             ];         default: // For any other action types             return state;     } }; 

5. Combining Reducers in Redux

If an application has multiple reducers, developers can use the redux library's combineReducers function to combine them into a single root reducer. Here's an example of how to combine reducers in Redux

JavaScript
// Combine multiple reducers into a single  // root reducer using combineReducers  import { combineReducers } from 'redux'; const rootReducer = combineReducers({     todos: todoReducer,     visibilityFilter: visibilityFilterReducer }); 

6. Connecting Components to Redux

Developers use the react-redux library's connect function to connect a Redux store to React components. Here's an example of a Redux store being linked to a React component:

JavaScript
// Connect a Redux store to a react component // using the connect    import { connect } from 'react-redux';  // Define functional components that accepts // todos as a prop const TodoList = ({ todos }) => (     <ul>         /* map over the todos array to render         each todo */         {todos.map((todo, index) => (             <li key={index}>{todo.text}</li>         ))}     </ul> ); const mapStateToProps = (state) => {     return {         // Specify which properties should          // be mapped to props         todos: state.todos     }; }; export default connect(mapStateToProps)(TodoList); 

Output:

Simplify State using Redux in React Example - Output
Output



Next Article
Getting Started with React

S

samarthshete14
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • React-Redux
  • React-Questions

Similar Reads

  • Getting Started with React
    ReactJS, often referred to as React, is a popular JavaScript library developed by Facebook for building user interfaces. It emphasizes a component-based architecture, where UIs are built using reusable components. React uses a declarative syntax to describe how UIs should look based on their state,
    10 min read
  • What Is Redux Change of State ?
    Before going to the topic "Redux Change of State" we should look at the question "What is Redux?". "A Predictable State Container for JS Apps" In other words, Redux is a pattern-based library that is used to manage and update the application state.  For managing and updating the application state Re
    14 min read
  • What is a store in Redux ?
    In Redux, the store is the central where all the state for the application is stored. This ensures the state is predictable and persistent for the entire application. is the central component for handling the state for the Redux application. Basics of Redux StoreThe Redux store is the heart of state
    6 min read
  • Persisting State in React App with Redux Persist
    Redux Persist is a library used to save the Redux store's state to persistent storage, such as local storage, and rehydrate it when the app loads. In this article, we make a simple counter application using React and Redux, demonstrating state persistence using Redux Persist. This project includes i
    3 min read
  • How to use Redux with ReactNative?
    First, we create a fresh ReactNative Project by running the command “npx react-native init reduxDemo”. You can also integrate Redux into your existing project. Go to the project folder by “cd {rootDirectory}/reduxDemo” and install dependencies. “npm install redux” which is an official redux dependen
    6 min read
  • What is Redux Saga?
    Redux Saga is 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 assistant t
    3 min read
  • How to Create Store in React Redux ?
    React Redux is a JavaScript library that is used to create and maintain state in React Applications efficiently. Here React Redux solves the problem by creating a redux store that stores the state and provides methods to use the state inside any component directly or to manipulate the state in a def
    4 min read
  • How to handle app state without Redux ?
    To handle app state without redux in a react application we will the the React JS hooks. For functional component hooks enable access and and manage data inside the states. PrerequisitesNPM & Node.jsReact JSReact useState Hook ApproachTo handle the app state without Redux we will maintain the st
    5 min read
  • Implementing React Router with Redux in React
    This article explores implementing React Router with Redux for enhanced state management and routing in React applications. Table of Content What is React Router and Redux?Approach to implement React Router in ReduxSteps to Setup the React AppWhat is React Router and Redux?React Router facilitates r
    3 min read
  • How to Manage State in Remix?
    Managing the state in Remix is one of the basics of making interactive applications. State management in Remix happens both on the client and the server, stitching server-side rendering together with client-side state management. Remix gives flexibility to handle the server-side or client-side state
    6 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