What is Redux

 Redux is a state management library commonly used with React for building complex and scalable web applications. It helps you manage the state of your application in a predictable and centralized way. Here's an explanation of Redux along with a diagram and a code snippet to illustrate its usage.



Redux explanation

  1. Store: The store is a centralized place where all of your application's state is kept. It's like a JavaScript object that holds the entire state tree of your application.
  2. Actions: Actions are plain JavaScript objects that describe what happened in your application. They are dispatched (triggered) to change the state. Actions are typically dispatched by components or other parts of your application.
  3.  Reducers: Reducers are functions that specify how the application's state changes in response to actions. They take the current state and an action as arguments and return the new state.
  4. Middleware: Middleware is a way to extend Redux with custom functionality. It can intercept actions, perform asynchronous operations, and modify actions before they reach the reducers.
  5. View: Your UI components, usually built with React, can subscribe to the Redux store to read data from it and re-render when the data changes.

Redux flow diagram



Step 1 :  

First, you need to install Redux and React-Redux

npm install redux react-redux


Step 2 :

Create a Redux store with a reducer:

  import { createStore } from 'redux';

// Define initial state
const initialState = {
  count: 0 // Assuming count is a property of your state
};

// Define the reducer function
const reducer = (state = initialState, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return { ...state, count: state.count + 1 };
    case 'DECREMENT':
      return { ...state, count: state.count - 1 };
    default:
      return state;
  }
};

// Create the Redux store
const store = createStore(reducer);

export default store;


Step 3:

Use the store in a React component

import React from 'react';
import { connect } from 'react-redux';

class Counter extends React.Component {
  render() {
    return (
      <div>
        <p>Count: {this.props.count}</p>
        <button onClick={this.props.increment}>Increment</button>
        <button onClick={this.props.decrement}>Decrement</button>
      </div>
    );
  }
}

// Define mapStateToProps to map state to props
const mapStateToProps = (state) => ({
  count: state.count
});

// Define mapDispatchToProps to map actions to props
const mapDispatchToProps = (dispatch) => ({
  increment: () => dispatch({ type: 'INCREMENT' }),
  decrement: () => dispatch({ type: 'DECREMENT' })
});

// Connect the component to the Redux store
export default connect(mapStateToProps, mapDispatchToProps)(Counter);


Step 4:

Wrap your app with the Redux Provider

import React from 'react';
import { Provider } from 'react-redux';
import store from './store'; // Corrected the path to store
import Counter from './Counter';

function App() {
  return (
    <Provider store={store}>
      <div className="App">
        <Counter />
      </div>
    </Provider> // Corrected the closing tag
  );
}

export default App;


Conclusion

In this example, the Redux store manages the state, and the Counter component is connected to the store using connect from react-redux. When you click the "Increment" or "Decrement" buttons, it dispatches actions to the store, and the state updates accordingly, causing the component to re-render.

Post a Comment

Previous Post Next Post