Skip to the content.

Back

Redux

Redux is a tool for managing state in JavaScript applications.

In React, the concept of props being passed from parent components down to child components is called flow. In a real application there could be many nested components. React doesn’t recommend direct component-to-component communication this way. It’s considered poor practice by many because direct component-to-component communication is error prone and leads to spaghetti code — an old term for code that is hard to follow.

This is where Redux comes in handy. Redux offers a solution of storing all your application state in one place, called a store.

Redux flow

The general concept of using store(s) to coordinate application state is a pattern known as the Flux pattern.

Three Principles

Redux can be described in three fundamental principles:

  1. Single Source of Truth Redux uses only one store for all its application state. Since all state resides in one place, Redux calls this the single source of truth.
  2. State is Read-Only According to Redux docs, “The only way to mutate the state is to emit an action, an object describing what happened.” This means the application cannot modify the state directly. Dispatching an action is the only way for the application code to express a state change.
  3. Changes are made with Pure Functions Reducers are functions that you write which handle dispatched actions and can actually change the state. A reducer takes in current state as an argument and can only modify the state by returning new state. Reducers should be written as “pure” functions, a term that describes a function with the following characteristics:
    • It does not make outside network or database calls.
    • Its return value depends solely on the values of its parameters.
    • Its arguments should be considered “immutable”, meaning they should not be changed.
    • Calling a pure function with the same set of arguments will always return the same value.

Flux vs MVC

MVC is an design pattern for client side and server side applications also , And Flux is a new application architecture from Facebook that promises the same as MVC, but with a different approach that focuses on unidirectional data flow.

MVC

MVC pattern

Good Point in MVC

Great separation of code easy to maintain.

Bad Point in MVC

In server Side, MVC is good,but in Client side most of the JS frameworks provide data binding support which let the view can talk with model directly, It shoudn’t be,Many times it become hard to debug something as there are scope for a property being changed by many ways.

MVC in client side

Facebook developers were facing problem scaling their MVC application and as a result of that the world got a new architecture flux.

Flux

Flux is the application architecture that Facebook uses for building client-side web applications. It complements React’s composable view components by utilizing a unidirectional data flow.

Flux

React-Redux “Flux” and Architecture

Dan Abramov felt that flux architecture could be simpler. Consequently, Dan Abramov & Andrew Clark developed Redux in 2015. Redux is a library, which implements the idea of Flux but in quite a different way. Redux architecture introduces new components like reducer and a centralized store.

Main differences

Redux-flux

To summarize:

Code example

References