• react-redux action传参 多个state处理


    action 中传递参数

    App.js 中 传递自己的参数

    1. function App (props){
    2. console.log(props,'===')
    3. return (
    4. <div>
    5. <h1>reduxh1>
    6. <button onClick={()=>{props.increment(10)}}>增加button>
    7. <p>{props.count}p>
    8. <button onClick={()=>{props.decrement(3)}}>减少button>
    9. div>
    10. )
    11. }

    action.js 传参

    1. export const increment = (num) => ({ type:'increment',payload:num })
    2. export const decrement = (num) => ({ type:'decrement',payload:num })

    reduce.js 中打印 action

    1. import { initstate } from "../state/state";
    2. //2.定义 reducer 第一个参数 state 第二个参数 action
    3. export function reducer(state = initstate,action){
    4. console.log(action,'===action')
    5. switch(action.type){
    6. case 'increment' :return {count:state.count + action.payload}
    7. break;
    8. case 'decrement' :return {count:state.count - action.payload}
    9. break;
    10. default :return state
    11. }
    12. }

    多个state状态

    增加一个新的state。 控制div 的背景颜色

    1. 定义color 组建
      1. function Color (props){
      2. let style = {
      3. width:100,
      4. height:100,
      5. background:props.color,
      6. textAlign:'center',
      7. lineHeight:100,
      8. }
      9. console.log('colorprops',props)
      10. return(
      11. <div>
      12. <button onClick={()=>{props.fngreen()}}>greenbutton>
      13. <button onClick={()=>{props.fnred()}}>redbutton>
      14. <div style={style}>多个 statediv>
      15. div>
      16. )
      17. }
      18. export default Color
    2. 定义state
      1. // 3.定义state
      2. export const initstate = {
      3. count:0
      4. }
      5. //color
      6. export const colorstate = {
      7. color:'red'
      8. }
    3. 定义action
      1. export const increment = (num) => ({ type:'increment',payload:num })
      2. export const decrement = (num) => ({ type:'decrement',payload:num })
      3. //处理color
      4. export const fngreen = () => ({ type:'fngreen'})
      5. export const fnred = () => ({ type:'fnred' })
    4. 定义reducer 处理color的reducer1
      1. import { colorstate } from "../state/state";
      2. //2.定义 reducer 第一个参数 state 第二个参数 action
      3. export function reducer(state = colorstate,action){
      4. console.log(action,'===color')
      5. switch(action.type){
      6. case 'fngreen' :return {color:'green' }
      7. break;
      8. case 'fnred' :return {color:'red'}
      9. break;
      10. default :return state
      11. }
      12. }
    5. store/index    创建store
      1. import {createStore} from 'redux'
      2. import{ reducer } from './reducer/reducer1'
      3. //1. 定义store
      4. let store = createStore( reducer )
      5. export default store
      6. console.log(store)
    6. color组建

      1. import { connect } from 'react-redux';
      2. import { bindActionCreators } from 'redux'
      3. import *as actionobj from '../store/action/action'
      4. function Color (props){
      5. let style = {
      6. width:100,
      7. height:100,
      8. background:props.color,
      9. textAlign:'center',
      10. lineHeight:100,
      11. }
      12. console.log('colorprops',props)
      13. return(
      14. <div>
      15. <button onClick={()=>{props.fngreen()}}>greenbutton>
      16. <button onClick={()=>{props.fnred()}}>redbutton>
      17. <div style={style}>多个 statediv>
      18. div>
      19. )
      20. }
      21. const mapStateToProps = function(state){
      22. return {color:state.color}
      23. }
      24. const mapDispatchToProps = (dispatch) => {
      25. return bindActionCreators(actionobj,dispatch)
      26. }
      27. export default connect(mapStateToProps,mapDispatchToProps)(Color);

     整合 reducer    combineReducers(reducers)

    redux/combineReducers.md at master · reduxjs/redux · GitHub

    多个reducer进行整合   reducer下创建index.js

     reducer/index.js

    1. import { combineReducers } from 'redux'
    2. import reducer1 from './reducer1'
    3. import reducer2 from './reducer2'
    4. export default combineReducers({
    5. reducer1,
    6. reducer2
    7. })

    reducer1.js

    1. import { colorstate } from "../state/state";
    2. //2.定义 reducer 第一个参数 state 第二个参数 action
    3. export default function reducer1(state = colorstate,action){
    4. console.log(action,'===color')
    5. switch(action.type){
    6. case 'fngreen' :
    7. return {color:'green' }
    8. break;
    9. case 'fnred' :
    10. return {color:'red'}
    11. break;
    12. default :return state
    13. }
    14. }

    reducer2.js

    1. import { initstate } from "../state/state";
    2. //2.定义 reducer 第一个参数 state 第二个参数 action
    3. export default function reducer2(state = initstate,action){
    4. console.log(action,'===action')
    5. switch(action.type){
    6. case 'increment' :return {count:state.count + action.payload}
    7. break;
    8. case 'decrement' :return {count:state.count - action.payload}
    9. break;
    10. default :return state
    11. }
    12. }

    store/index.js

    1. import {createStore} from 'redux'
    2. import reducer from './reducer'
    3. //1. 定义store
    4. let store = createStore( reducer )
    5. export default store

    App.js 

    注意:combineReducers   返回的结果是一个对象

    1. {
    2. reducer1:{color:'red'},
    3. reducer2:{count:0}
    4. }

    所以在使用的。候需要。{props.reducer2.count}   background:props.reducer1.color, 

    映射的时候需要解构

     

    reducer1.js. 和reducer2.js  解构state

    1. import { colorstate } from "../state/state";
    2. //2.定义 reducer 第一个参数 state 第二个参数 action
    3. export default function reducer1(state = colorstate,action){
    4. console.log(action,'===color')
    5. switch(action.type){
    6. case 'fngreen' :
    7. return {...state,color:'green' }
    8. break;
    9. case 'fnred' :
    10. return {...state,color:'red'}
    11. break;
    12. default :return state
    13. }
    14. }
    1. import { initstate } from "../state/state";
    2. //2.定义 reducer 第一个参数 state 第二个参数 action
    3. export default function reducer2(state = initstate,action){
    4. console.log(action,'===action')
    5. switch(action.type){
    6. case 'increment' :return {...state,count:state.count + action.payload}
    7. break;
    8. case 'decrement' :return {...state,count:state.count - action.payload}
    9. break;
    10. default :return state
    11. }
    12. }

  • 相关阅读:
    Linux进程控制(1)
    在Ubuntu20.04安装StarRocks On Docker并在DataGrip配置JDBC协议连接容器内StarRocks2.3.2
    技术周总结2024.06.03~06.09(K8S & HikariCP数据库连接池)
    HDU-1754 I Hate It(线段树单点更新,维护区间最大值)
    Python用正则化Lasso、岭回归预测房价、随机森林交叉验证鸢尾花数据可视化2案例|数据分享...
    新手小白前端学习艰辛之路
    GBASE 8s中onshutdown 脚本的用法
    大前端 业务架构 插件库 设计模式 属性 线程
    SpringBoot 整合 Quartz 实现 对任务进行CRUD
    Spark SQL自定义collect_list分组排序
  • 原文地址:https://blog.csdn.net/weixin_41040445/article/details/125906957