DEV Community

Prashant Pandey
Prashant Pandey

Posted on

1

Redux Concept

These days I am revising some concept which I learned while doing some projects. These concepts are useful if you are creating any website using react or some other technology. So we know that react uses props and states to manage the design and flow of data on any website. When working on small projects with a simple tree structure, it is easier to maintain the states of the components and pass the data between the components. For this, the data needs to reside in a single component so that it can be passed on to the sibling components. The method needed to update the state also needs to reside in the parent component and be passed to the sibling components as props. So state management gets messy when the app gets complex.

Here comes the concept called redux. Redux is an open-source JavaScript library for managing application state.

Image From https://miro.medium.com/max/1086/1*a15Fjk16s4CVlfHaTkO73w.png

There are some terminology for the Redux.

  • Types
  • Reducers
  • Actions

First of all, we need to create a store where our states will store. To change something in the state, we need to dispatch an action. Actions get the data (or something accordingly) and according to the data, it will call the reducers to change or update the state. Lets understand with an example.

import { createStore } from 'redux';
const USER_LOADING = 'USER_LOADING';

const initialState = {
  isLoading: false,
};

function rootReducer(state = initialState, action) {
  switch (action.type) {
    case USER_LOADING:
      return {
        ...state,
        isLoading: true,
        error: null,
      };

    default:
      return state;
  }
}

const loading = () => (dispatch, getState) => {    //This is action.
  dispatch({
    type: USER_LOADING,
  });
};

const store = createStore(
  rootReducer,
);


Enter fullscreen mode Exit fullscreen mode

So here in this example if we need to change the state of isLoading state to true we just need to call the action loading() in the component and it will automatically update the state for all.
This is it for now, if you have any question you can dm me.
Contact shared in my portfolio.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay