温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

react-redux action传参及多个state处理如何实现

发布时间:2022-07-28 09:54:04 来源:亿速云 阅读:184 作者:iii 栏目:开发技术

这篇文章主要讲解了“react-redux action传参及多个state处理如何实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“react-redux action传参及多个state处理如何实现”吧!

action 中传递参数

App.js 中 传递自己的参数

function App (props){
  console.log(props,'===')
  return (
    <div>
      <h2>redux</h2>
      <button onClick={()=>{props.increment(10)}}>增加</button>
      <p>{props.count}</p>
      <button onClick={()=>{props.decrement(3)}}>减少</button>
    </div>
  )
}

action.js 传参

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

reduce.js 中打印 action

react-redux action传参及多个state处理如何实现

import { initstate } from "../state/state";
  //2.定义 reducer  第一个参数  state  第二个参数 action
 export  function reducer(state = initstate,action){
    console.log(action,'===action')
    switch(action.type){
      case 'increment' :return {count:state.count + action.payload}
      break;
      case 'decrement' :return {count:state.count - action.payload}
      break;
      default :return state
    }
  }

多个state状态

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

定义color 组建

function Color (props){
    let style = {
        width:100,
        height:100,
        background:props.color,
        textAlign:'center',
        lineHeight:100,
    }
    console.log('colorprops',props)
    return(
        <div>
            <button onClick={()=>{props.fngreen()}}>green</button>
            <button onClick={()=>{props.fnred()}}>red</button>
            <div style={style}>多个 state</div>
        </div>
    )
}
export default Color

定义state

// 3.定义state
export const  initstate = {
    count:0
}
//color
export const  colorstate = {
    color:'red'
}

定义action

export  const increment = (num) => ({ type:'increment',payload:num })
export  const decrement = (num) => ({ type:'decrement',payload:num })
//处理color
export  const fngreen = () => ({ type:'fngreen'})
export  const fnred = () => ({ type:'fnred' })

定义reducer 处理color的reducer1

import { colorstate } from "../state/state";
  //2.定义 reducer  第一个参数  state  第二个参数 action
 export  function reducer(state = colorstate,action){
    console.log(action,'===color')
    switch(action.type){
      case 'fngreen' :return {color:'green' }
      break;
      case 'fnred' :return {color:'red'}
      break;
      default :return state
    }
  }

store/index    创建store

import {createStore} from 'redux'
import{ reducer } from './reducer/reducer1'
  //1. 定义store
  let store = createStore( reducer )
  export default store 
  console.log(store)

color组建

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux'
import *as actionobj from '../store/action/action'
function Color (props){
    let style = {
        width:100,
        height:100,
        background:props.color,
        textAlign:'center',
        lineHeight:100,
    }
    console.log('colorprops',props)
    return(
        <div>
            <button onClick={()=>{props.fngreen()}}>green</button>
            <button onClick={()=>{props.fnred()}}>red</button>
            <div style={style}>多个 state</div>
        </div>
    )
}
const mapStateToProps = function(state){
    return {color:state.color}
}
    const mapDispatchToProps = (dispatch) => {
    return bindActionCreators(actionobj,dispatch)
}
export default connect(mapStateToProps,mapDispatchToProps)(Color);

react-redux action传参及多个state处理如何实现

 整合 reducer    combineReducers(reducers)

redux/combineReducers.md at master &middot; reduxjs/redux &middot; GitHub

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

react-redux action传参及多个state处理如何实现

 reducer/index.js

import { combineReducers } from 'redux'
import reducer1 from './reducer1'
import reducer2 from './reducer2'
export default combineReducers({
    reducer1,
    reducer2
})

reducer1.js

import { colorstate } from "../state/state";
//2.定义 reducer  第一个参数  state  第二个参数 action
export default function reducer1(state = colorstate,action){
    console.log(action,'===color')
    switch(action.type){
      case 'fngreen' :
          return {color:'green' }  
      break;
      case 'fnred' :
          return {color:'red'}  
      break;
      default :return state
    }
}

reducer2.js

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

store/index.js

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

App.js 

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

{
    reducer1:{color:'red'},
    reducer2:{count:0}
}

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

映射的时候需要解构

react-redux action传参及多个state处理如何实现

react-redux action传参及多个state处理如何实现

reducer1.js. 和reducer2.js  解构state

import { colorstate } from "../state/state";
//2.定义 reducer  第一个参数  state  第二个参数 action
export default function reducer1(state = colorstate,action){
    console.log(action,'===color')
    switch(action.type){
      case 'fngreen' :
          return {...state,color:'green' }  
      break;
      case 'fnred' :
          return {...state,color:'red'}  
      break;
      default :return state
    }
}
import { initstate } from "../state/state";
  //2.定义 reducer  第一个参数  state  第二个参数 action
 export default  function reducer2(state = initstate,action){
    console.log(action,'===action')
    switch(action.type){
      case 'increment' :return {...state,count:state.count + action.payload}
      break;
      case 'decrement' :return {...state,count:state.count - action.payload}
      break;
      default :return state
    }
  }

感谢各位的阅读,以上就是“react-redux action传参及多个state处理如何实现”的内容了,经过本文的学习后,相信大家对react-redux action传参及多个state处理如何实现这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI