DEV Community

Cover image for Redux Principles
Raushan Kumar
Raushan Kumar

Posted on

2

Redux Principles

Redux is a predictable state container. It stores the state of your application. It stores and manages the application state.

In redux, all state transitions are explicit and is possible to keep track of them.

Basically, there are three principles of redux:

1. First Principle:

"The state of your whole application is stored in an object tree within a single store"

Maintain our application state in a single object which would be managed by the Redux store.

example:
Let's assume we are tracking the number of ice-cream at the ice-cream parlor

{
   numberOfIceCreams: 10
}

2. Second Principle:

"The only way to change the state is to emit an action, an object describing what happened"

To update the state of your app, you need to let redux know about that with action.

example:
Let the shopkeeper know about action - 'BUY_ICECREAM'

{
   type: BUY_ICECREAM
}

3. Third Principle:

"To specify how the state tree is transformed by actions, you write pure reducers"

Reducers - (prevState, action) => newState

example:
Reducer is the shopkeeper.

const reducer = (state, action) => {
   switch(action.type) {
     case BUY_ICECREAM:
        return {
           numberOfIceCreams: state.numberOfIceCreams - 1
         }
     }
}

Thank You, Happy Coding!!

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay