• 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状态管理器并订阅状态更改。

  • 相关阅读:
    基于DJYOS的图形界面编程--DJYGUI系列教程
    ElasticSearch知识点
    使用Python进行网站页面开发——Django快速入门
    javascript的动态作用域和静态作用域
    DAP数据加工流程梳理
    2021春招Java面试题大全(精华)
    Docker手把手教程(四)Dockerfile完全指南
    SpringBoot中有几种定义Bean的方式?
    打工人办公环境布置篇
    递归的应用
  • 原文地址:https://blog.csdn.net/luoxiaonuan_hi/article/details/132775017