React Training
  • React JS Library
  • Roadmap
  • Training OutLine
  • React js basics
    • Understanding React JS
    • React JS a framework?
    • Setting Up React
    • Say Hello to React
    • Everything is a Component
    • Create-react-app
  • React Building Blocks
    • JSX and Babel
    • One Way Data Flow
    • Virtual DOM
    • V of MVC
    • React Terminology
    • React Tooling
  • Day 01
    • Day 01 OutLine
    • All About JSX
    • React Tools/ npm & webpack
    • Introduction of Webpack
    • Hello world using webpack
      • Webpack Setting up with React
    • NPM Scripts | Package JSON
      • Package.json file
    • React JS Principles
      • One Way Data Flow
      • V of MVC
      • Virtual DOM
    • Create React App - Part-1
    • Create React App - Part-2
  • Day 02
    • Quick Recap
      • Quiz
    • State & Props
      • State & Props in Depth
      • State Vs Props | Compare
    • React LifeCycle Methods
      • React LifeCycle Methods for V-0.16 x
    • Constructor | Lifecycle
    • Write Flicker App | First App
  • Day 03
    • Quick Recap
    • Life Cycle Flow
      • Birth and Mounting
      • Initlization and Construction
      • Pre Mounting
      • Render Method
      • componentDidMount
    • Type of React Components
      • Examples- Quick Compare
      • Class and Functional components
      • Functional Components
    • YouTube application
      • Component Design
    • All in One LifeCycle
  • Assignment
    • React App development
  • Day 04
    • Quick Recap on Lifecycle
    • Lifecycle deprecated/New Methods
      • New Lifecycle Methods
    • Lets Build App Netflix | Mock
  • Assignment
    • Github battle App | Assignment
  • Day 05
    • Quick Recap : Hooks
    • ES6 Features | Hands-on
      • ES6 Code Examples
    • Next Stop - React Router | SPA
      • Code examples | Router
      • React Router Building Blocks
      • Application using react-router-dom
  • Day 06
    • Router V4 | Quick Recap
    • ES2015 | 16 Quick Recap
    • LifeCycle Methods -Part-1
    • LifeCycle Methods -Part-2
  • Day 07
    • Quick Recap | New Lifecycle
    • Quick Recap | React Routing
    • Context API | React JS
      • component with context APIs
      • Context API | examples
    • App using Hooks/Context APIs
  • Assignment
    • Assignments
  • State Management Day-08
    • Quick Recap
    • Managing React State
      • What is Redux
      • Understanding Redux
      • Hello World "Redux"
  • React Redux Day - 09
    • Redux State Manager
    • Redux Redux Development
    • Simple Application | Redux
  • Redux Live Application Day -10
    • Redux with existing Application
      • Redux with React App
      • Lets Build More Apps
      • Should I use Redux from Dan
    • Quick Look at JS in React
    • Learn By Reading
  • miscellaneous Items - Day 11
    • Hooks useReducer
    • Hooks useContext
    • Hooks useRef
    • Hooks useEffect
    • Hooks useState
    • Lazy Loading and code splitting
    • Styling React Component
  • React Next Step - Day 12
    • Topics
    • Jest and Enjyme Testing
    • Examples: Testing
  • React Native
    • What is React Native
    • Setting up Environment
      • Linux Systems
    • React Native Hello World App
    • React Native Architecture
    • React Native Vs Native
    • Expo Cli or React Native CLI
  • React Native core Fundamental
    • React Native "Hello World"
    • Course OutLine
    • Getting started with Expo App
    • Layout with Flexbox
    • Working with Styles and Events
    • Manging Component State and Props
    • Build Simple Task List Application
  • What to Debug & How to Debug
    • Debug React Native Application
Powered by GitBook
On this page
  • Redux is a predictable state container for JavaScript apps.
  • Installation
  • Redux Starter Kit
  • Basic Example

Was this helpful?

  1. State Management Day-08
  2. Managing React State

What is Redux

PreviousManaging React StateNextUnderstanding Redux

Last updated 5 years ago

Was this helpful?

Redux is nothing but a storehouse which contains the state of the application. It becomes a painful task when the size of application becomes large to manage the state of each component in your application. So redux comes to our rescue in doing so and maintaining and updating the state of each component in our application.

Redux is often found confusing when we try our hands on it for the first time. So I will quote an example to make you understand what redux is and why do we need redux at all.

In react application there is unidirectional data flow by unidirectional I mean data flows from parent components to child components not the other way around so you send the data from parent components to child components in the form of what we call as props then this child component make use of this prop.

In react application everything is a component Imagine how difficult it becomes if there are too many components in your application like the one in the image given below

so it becomes difficult to manage the flow of data from parent to child components. This is the first reason why we use redux as it manages the states of all the components for us.

Now coming back to the what is redux part?

Redux is an open-source JavaScript library for managing application state

There are few concepts which you need to understand for understanding redux properly I will try to explain them with the help of an example.

Let’s say you ordered pair of shoes from flipkart after ordering the shoes you get the delivery from the delivery agent in a specified time. So here your ordering of shoes is a action which is one of the concepts of redux.

After you perform an action here in this case ordering of shoes is an action you wait for the delivery but does it happen like that as soon as you order something from flipkart you get the delivery straight away. No in reality it takes time and there is a process which gets followed everytime when you order something from your favorite website.

So similary in redux after performing the action there is a term called dispatch that sends your action to reducer. Just like after placing the order your package is shipped to nearest warehouse to your address. The same work in redux is done by dispatch.

Now Reducer looks at the action and accordingly do what it needs to do for storing the data in store. Reducer is nothing but a file consisting of switch case statement and used for storing the data in a store and returning the updated state value from the store. So whenever the state is updated the value in the store gets updated too.

By this way we eventually keep the state of the application in a store. Below is a hello world implementation in react using redux.

Redux is a predictable state container for JavaScript apps.

Installation

Redux is available as a package on NPM for use with a module bundler or in a Node application:

npm install --save redux

Redux Starter Kit

Basic Example

The whole state of your app is stored in an object tree inside a single store. The only way to change the state tree is to emit an action, an object describing what happened. To specify how the actions transform the state tree, you write pure reducers.

That's it!

import { createStore } from 'redux'
function counter(state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    default:
      return state
  }
}
let store = createStore(counter)

store.subscribe(() => console.log(store.getState()))

// The only way to mutate the internal state is to dispatch an action.
// The actions can be serialized, logged or stored and later replayed.
store.dispatch({ type: 'INCREMENT' })
store.dispatch({ type: 'INCREMENT' })
store.dispatch({ type: 'DECREMENT' })

Instead of mutating the state directly, you specify the mutations you want to happen with plain objects called actions. Then you write a special function called a reducer to decide how every action transforms the entire application's state.

In a typical Redux app, there is just a single store with a single root reducing function. As your app grows, you split the root reducer into smaller reducers independently operating on the different parts of the state tree. This is exactly like how there is just one root component in a React app, but it is composed out of many small components.

It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as .

You can use Redux together with , or with any other view library. It is tiny (2kB, including dependencies), but has a large ecosystem of addons available.

It is also available as a precompiled UMD package that defines a window.Redux global variable. The UMD package can be used as a directly.

For more details, see the page.

Redux itself is small and un-opinionated. We also have a separate package called , which includes some opinionated defaults that help you use Redux more effectively.

It helps simplify a lot of common use cases, including , , and even .

Whether you're a brand new Redux user setting up your first project, or an experienced user who wants to simplify an existing application, can help you make your Redux code better.

live code editing combined with a time traveling debugger
React
<script> tag
Installation
redux-starter-kit
store setup
creating reducers and writing immutable update logic
creating entire "slices" of state at once
redux-starter-kit
Redux