Open In App

Introduction to React-Redux

The ones familiar with react will know that react is a component-based front end library tool that connects the various segments of the web page. In react, we use props (short for properties) in a component which allows the usage of non-static variables. With the help of props, we can pass these variables into various other components (child components) from the parent component.

Example: Passing props from parent component to child component: 






import React, { Component } from 'react';
class App extends Component {
  render() {
    const wishes = 'Welcome to My World';
    return (
      <div>
        <Greeting wishes={wishes} />
      </div>
    );
  }
}
class Greeting extends Component {
  render() {
    return <h1>Welcome to {this.props.wishes} world!!</h1>;
  }
}
export default App;

The presence of the state in react allows defining its own variables of a component. With a few components in our application, we can pass these states as props to the child components. But as the count of components increases in the application in accordance with its large objective, we need to pass these state to other components located far away from each other in the component tree. At this moment, the usual way of transferring state as props turns out to be complex as not every component is a parent to the component requiring the state.

Example: Initializing state in a component and passing it as props to its child component:  






class Parent extends Component{
 constructor(props){
  super(props);
  this.state={
   child1=0;
   child2=1;
  };
 }
 render()
 {
  return (
   <Child1 value={this.state.child1} />
   <Child2 value={this.state.child2} />
  );
 }
}

This brings the need for react-redux in our react application. React-redux being a state management tool makes it easier to pass these states from one component to another irrespective of their position in the component tree and hence prevents the complexity of the application.

Working Procedure: When a react application holds various components in need of state from other components it becomes difficult to realize where the state should reside among these components to make it easier to maintain. React-redux provides a store which makes the state inside components easier to maintain. Along with stores, react-redux introduces actions and reducers which work simultaneously with stores to make the state more predictable. The working of the features in react-redux is explained below: 




export const increment = (num) => {
    return{
        type: 'INCREMENT',
        payload: num
    };
};
 
export const decrement = () => {
    return{
        type: 'DECREMENT',
    };
};




const counterReducer = (state=0, action) => {
 switch(action.type)
 {
  case 'INCREMENT':
   return state + action.payload;
  case 'DECREMENT':
   return state - 1;
  default:
   return state;
 }
};




import {createStore} from 'react-redux';
 
const store = createStore(
 counterReducer,
 window.__REDUX__DEVTOOLS__EXTENSIONS__ &&
    window.__REDUX__DEVTOOLS__EXTENSION__()
);

React-Redux needed for: As shown above, it is clearly noticeable that the usage of react-redux introduces a lot of boilerplate code in our application only for the purpose of passing the state from one component to the other. This may lead many new learners of react-redux to withdraw their interest from the same. Regardless of this fact many developers prefer react-redux in their application due to the following reasons: 

Installing: To install react-redux in your react app, run the below command in the terminal:  

npm install react-redux

Reference: https://react-redux.js.org/
 


Article Tags :