How to use redux

Redux is a state management library that is commonly used with React. It helps you better manage the state of your application while also making your code more predictable and maintainable. Here are the general steps for using Redux:

1. Install Redux
You can use npm or yarn to install Redux:

npm install redux

or

yarn add redux

2. Create a Store
In Redux, the store is the single source of all state in the application. In order to create a store, you need to pass in a reducer through the createStore function provided by Redux, for example:

import {
    
     createStore } from 'redux';

const initialState = {
    
    
  count: 0
};

function 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;
  }
}

const store = createStore(reducer);

In the above example, we defined a function called reducer that will receive the current state and an operation type as parameters and return the new state. Next, we pass this reducer into the createStore function to create a Redux store.

3. Use a Provider to wrap the root component.
When using Redux in a React application, you need to use a Provider component to pass the store to all child components. For example:

import React from 'react';
import ReactDOM from 'react-dom';
import {
    
     Provider } from 'react-redux';
import App from './App';
import store from './store';

ReactDOM.render(
  <Provider store={
    
    store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

In the above example, we pass the store as props to the Provider component and nest the Provider component in the root component App.

4. Connect the component to the Redux store.
To connect the React component to the Redux store, you can use the connect function provided by React Redux. The connect function will receive two parameters: mapStateToProps and mapDispatchToProps.

The mapStateToProps function allows you to map state in the Redux store to component props. For example:

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

function Counter(props) {
    
    
  return (
    <div>
      <p>{
    
    props.count}</p>
      <button onClick={
    
    props.increment}>+</button>
      <button onClick={
    
    props.decrement}>-</button>
    </div>
  );
}

function mapStateToProps(state) {
    
    
  return {
    
    
    count: state.count
  };
}

function mapDispatchToProps(dispatch) {
    
    
  return {
    
    
    increment: () => dispatch({
    
     type: 'INCREMENT' }),
    decrement: () => dispatch({
    
     type: 'DECREMENT' })
  };
}

export default connect(mapStateToProps, mapDispatchToProps)(Counter);

In the above example, we defined a component called Counter and connected it to the Redux store. We use the mapStateToProps function to map the count property from the store to the component's props so that the component can access and display the current counter value. We also use the mapDispatchToProps function to map the increment and decrement functions to the component's props so that the component can trigger the corresponding Redux operation.

5. Trigger Redux operations
In Redux, you can trigger operations by dispatching an object containing the type attribute. For example:

store.dispatch({
    
     type: 'INCREMENT' });

In the above example, we trigger an operation by calling the store's dispatch method and passing an object containing a type attribute of INCREMENT. This will cause the store to run the reducer function and update the state in the application.

The above are the general steps for using Redux. Of course, the actual situation may change due to different application scenarios.

Guess you like

Origin blog.csdn.net/weixin_43534452/article/details/131418983