• react-redux使用


     1.创建store文件夹,分别创建子文件夹actions和reducers

    2。创建constant.js

    1. /*
    2. 该模块是用于定义action对象中type类型的常量值,目的只有一个:便于管理的同时防止程序员单词写错
    3. */
    4. export const CHANGEVALUE = 'change_value'

    3.store.js

    1. import { legacy_createStore as createStore, applyMiddleware } from "redux";
    2. import thunk from "redux-thunk";
    3. import reducers from "./reducers";
    4. export default createStore(reducers, applyMiddleware(thunk))

    4.reducers创建index.js和redux_test.js

    redux_test.js

    1. import { CHANGEVALUE } from "../constant";
    2. //定义一个默认状态
    3. const defaultState = {
    4. msg: "你好,世界"
    5. }
    6. //导出一个函数
    7. // eslint-disable-next-line import/no-anonymous-default-export
    8. export default (state = defaultState, action) => {
    9. //从action对象中获取:type、data
    10. const { type, data } = action
    11. let newState = JSON.parse(JSON.stringify(state))//使用深拷贝,不然数据不变化
    12. switch (type) {
    13. case CHANGEVALUE:
    14. newState.msg = state.msg + data;
    15. break
    16. default:
    17. break
    18. }
    19. return newState
    20. }

    index.js

    1. /*
    2.     该文件用于汇总所有的reducer为一个总的reducer
    3. */
    4. //引入combineReducers,用于汇总多个reducer
    5. import { combineReducers } from 'redux'
    6. import redux_test from './redux_test'
    7. //汇总所有的reducer变为一个总的reducer
    8. export default combineReducers({
    9.     redux_test
    10. })

    5.reducers创建redux_test.js

    1. import { CHANGEVALUE } from "../constant"
    2. /*
    3. 该文件专门为Count组件生成action对象
    4. */
    5. //同步action,就是指action的值为Object类型的一般对象
    6. export const changeValue = data => ({ type: CHANGEVALUE, data })

    测试=====

    index.js使用Provider监听redux,传入store

    1. import React from 'react';
    2. import ReactDOM from 'react-dom/client';
    3. import { Provider } from 'react-redux';//redux
    4. import App from './App';
    5. import store from './store/store';
    6. const root = ReactDOM.createRoot(document.getElementById('root'));
    7. root.render(
    8. /* 此处需要用Provider包裹App,目的是让App所有的后代容器组件都能接收到store */
    9. <Provider store={store}>
    10. <App />
    11. Provider>
    12. );

     所有使用redux的组件放在containers目录下

    创建ReduxTest组件,redux父组件与子组件使用connect连接

    1. import React, { Fragment } from 'react'
    2. import { connect } from 'react-redux'
    3. import { changeValue } from '../../store/actions/redux_test'
    4. function ReduxTest(props) {
    5. return (
    6. <Fragment>
    7. <div>
    8. {props.msg}
    9. div>
    10. <button onClick={() => props.changeValue('----->hello')}>修改显示button>
    11. Fragment>
    12. )
    13. }
    14. //使用connect()()创建并暴露一个Count的容器组件
    15. export default connect(
    16. state => ({
    17. msg: state.redux_test.msg
    18. }),
    19. {
    20. changeValue
    21. }
    22. )(ReduxTest)

  • 相关阅读:
    C++——类和对象讲解
    Ylearn因果推断入门实践——Kaggle银行客户流失
    【web-渗透测试方法】(15.5)测试访问控件
    Zookeeper篇---第三篇
    第136篇 库合约
    单链表(一篇带你掌握单链表)
    SAP S/4 HANA 与R3(ECC) 的区别
    redis探索之缓存击穿、缓存雪崩、缓存穿透
    LINUX初级 总结
    mysql索引不生效
  • 原文地址:https://blog.csdn.net/zhaokai621/article/details/126057848