• React-Redux 老版本 connext的使用记录(只为记录)


    注:新版 redux 用法中 使用useSelector、useDispatch替代connect,这里只做老用法的记录使用。
    ​Redux最新用法参考 个人React专栏react初级学习

    React-Redux是一个用于将React应用程序连接到Redux状态管理器的库。以下是React-Redux的简单用法示例:

    1. 安装React-Redux库
    npm install react-redux
    
    • 1
    1. 创建Redux store

    创建Redux store,这是存储应用程序状态的地方。可以使用Redux createStore函数创建store。

    import { createStore } from 'redux';
    
    const initialState = {
      count: 0
    };
    
    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;
      }
    };
    
    const store = createStore(reducer);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 创建React组件

    创建一个React组件,将组件中需要的store中的状态传递给组件的props。可以使用React Redux提供的connect函数将组件连接到Redux store并订阅state更改。

    import React from 'react';
    import { connect } from 'react-redux';
    
    const Counter = ({ count, increment, decrement }) => (
      

    Count: {count}

    ); const mapStateToProps = state => ({ count: state.count }); const mapDispatchToProps = { increment: () => ({ type: 'INCREMENT' }), decrement: () => ({ type: 'DECREMENT' }) }; export default connect( mapStateToProps, mapDispatchToProps )(Counter);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 渲染组件

    将组件渲染到DOM中,并将store传递给React应用程序的Provider组件。

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Provider } from 'react-redux';
    import Counter from './Counter';
    import store from './store';
    
    ReactDOM.render(
      
        
      ,
      document.getElementById('root')
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    以上示例中Counter组件可以读取store中的状态,并且可以分发操作来更改store中的状态。通过这种方式,React组件可以轻松连接到Redux状态管理器并订阅状态更改。React-Redux是一个用于将React应用程序连接到Redux状态管理器的库。以下是React-Redux的简单用法示例:

    1. 安装React-Redux库
    npm install react-redux
    
    • 1
    1. 创建Redux store

    创建Redux store,这是存储应用程序状态的地方。可以使用Redux createStore函数创建store。

    import { createStore } from 'redux';
    
    const initialState = {
      count: 0
    };
    
    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;
      }
    };
    
    const store = createStore(reducer);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 创建React组件

    创建一个React组件,将组件中需要的store中的状态传递给组件的props。可以使用React Redux提供的connect函数将组件连接到Redux store并订阅state更改。

    import React from 'react';
    import { connect } from 'react-redux';
    
    const Counter = ({ count, increment, decrement }) => (
      

    Count: {count}

    ); const mapStateToProps = state => ({ count: state.count }); const mapDispatchToProps = { increment: () => ({ type: 'INCREMENT' }), decrement: () => ({ type: 'DECREMENT' }) }; export default connect( mapStateToProps, mapDispatchToProps )(Counter);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 渲染组件

    将组件渲染到DOM中,并将store传递给React应用程序的Provider组件。

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Provider } from 'react-redux';
    import Counter from './Counter';
    import store from './store';
    
    ReactDOM.render(
      
        
      ,
      document.getElementById('root')
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    以上示例中Counter组件可以读取store中的状态,并且可以分发操作来更改store中的状态。通过这种方式,React组件可以轻松连接到Redux状态管理器并订阅状态更改。React-Redux是一个用于将React应用程序连接到Redux状态管理器的库。以下是React-Redux的简单用法示例:

    1. 安装React-Redux库
    npm install react-redux
    
    • 1
    1. 创建Redux store

    创建Redux store,这是存储应用程序状态的地方。可以使用Redux createStore函数创建store。

    import { createStore } from 'redux';
    
    const initialState = {
      count: 0
    };
    
    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;
      }
    };
    
    const store = createStore(reducer);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 创建React组件

    创建一个React组件,将组件中需要的store中的状态传递给组件的props。可以使用React Redux提供的connect函数将组件连接到Redux store并订阅state更改。

    import React from 'react';
    import { connect } from 'react-redux';
    
    const Counter = ({ count, increment, decrement }) => (
      

    Count: {count}

    ); const mapStateToProps = state => ({ count: state.count }); const mapDispatchToProps = { increment: () => ({ type: 'INCREMENT' }), decrement: () => ({ type: 'DECREMENT' }) }; export default connect( mapStateToProps, mapDispatchToProps )(Counter);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 渲染组件

    将组件渲染到DOM中,并将store传递给React应用程序的Provider组件。

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Provider } from 'react-redux';
    import Counter from './Counter';
    import store from './store';
    
    ReactDOM.render(
      
        
      ,
      document.getElementById('root')
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    以上示例中Counter组件可以读取store中的状态,并且可以分发操作来更改store中的状态。通过这种方式,React组件可以轻松连接到Redux状态管理器并订阅状态更改。React-Redux是一个用于将React应用程序连接到Redux状态管理器的库。以下是React-Redux的简单用法示例:

    1. 安装React-Redux库
    npm install react-redux
    
    • 1
    1. 创建Redux store

    创建Redux store,这是存储应用程序状态的地方。可以使用Redux createStore函数创建store。

    import { createStore } from 'redux';
    
    const initialState = {
      count: 0
    };
    
    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;
      }
    };
    
    const store = createStore(reducer);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 创建React组件

    创建一个React组件,将组件中需要的store中的状态传递给组件的props。可以使用React Redux提供的connect函数将组件连接到Redux store并订阅state更改。

    import React from 'react';
    import { connect } from 'react-redux';
    
    const Counter = ({ count, increment, decrement }) => (
      

    Count: {count}

    ); const mapStateToProps = state => ({ count: state.count }); const mapDispatchToProps = { increment: () => ({ type: 'INCREMENT' }), decrement: () => ({ type: 'DECREMENT' }) }; export default connect( mapStateToProps, mapDispatchToProps )(Counter);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 渲染组件

    将组件渲染到DOM中,并将store传递给React应用程序的Provider组件。

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Provider } from 'react-redux';
    import Counter from './Counter';
    import store from './store';
    
    ReactDOM.render(
      
        
      ,
      document.getElementById('root')
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    以上示例中Counter组件可以读取store中的状态,并且可以分发操作来更改store中的状态。通过这种方式,React组件可以轻松连接到Redux状态管理器并订阅状态更改。

  • 相关阅读:
    应用案例 | 使用dataFEED OPC Suite将汽车零部件工厂数据集成到SAP Business Suite
    准备大半年,面试频繁受挫,Java岗面试为何越来越难?
    Redis从理论到实战:使用Redis实现商铺查询缓存(逐步分析缓存更新策略)
    华为交换机S200, S1700系列产品命名规则
    线程——进程与线程——day10
    懒人福音:Java版Webhook机器人,一键开启‘躺赢’模式
    第一章 Redis基础
    OpenCV项目开发实战--实现面部情绪识别对情绪进行识别和分类及详细讲解及完整代码实现
    【11.23+11.24】Codeforces 刷题
    基于花授粉优化的BP神经网络(分类应用) - 附代码
  • 原文地址:https://blog.csdn.net/luoxiaonuan_hi/article/details/132775017