欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react-redux action傳參及多個state處理的實現(xiàn)

 更新時間:2022年07月27日 15:19:56   作者:敢問  
本文主要介紹了react-redux action傳參及多個state處理的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

action 中傳遞參數(shù)

App.js 中 傳遞自己的參數(shù)

function App (props){
  console.log(props,'===')
  return (
    <div>
      <h1>redux</h1>
      <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

import { initstate } from "../state/state";
  //2.定義 reducer  第一個參數(shù)  state  第二個參數(shù) 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狀態(tài)

增加一個新的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  第一個參數(shù)  state  第二個參數(shù) 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    創(chuàng)建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);

 整合 reducer    combineReducers(reducers)

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

多個reducer進行整合   reducer下創(chuàng)建index.js

 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  第一個參數(shù)  state  第二個參數(shù) 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  第一個參數(shù)  state  第二個參數(shù) 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   返回的結(jié)果是一個對象

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

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

映射的時候需要解構(gòu)

reducer1.js. 和reducer2.js  解構(gòu)state

import { colorstate } from "../state/state";
//2.定義 reducer  第一個參數(shù)  state  第二個參數(shù) 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  第一個參數(shù)  state  第二個參數(shù) 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
    }
  }

到此這篇關(guān)于react-redux action傳參及多個state處理的實現(xiàn)的文章就介紹到這了,更多相關(guān)react-redux action傳參及多個state處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解基于React.js和Node.js的SSR實現(xiàn)方案

    詳解基于React.js和Node.js的SSR實現(xiàn)方案

    這篇文章主要介紹了詳解基于React.js和Node.js的SSR實現(xiàn)方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • React循環(huán)遍歷渲染數(shù)組和對象元素方式

    React循環(huán)遍歷渲染數(shù)組和對象元素方式

    這篇文章主要介紹了React循環(huán)遍歷渲染數(shù)組和對象元素方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • React使用hook如何實現(xiàn)數(shù)據(jù)雙向綁定

    React使用hook如何實現(xiàn)數(shù)據(jù)雙向綁定

    這篇文章主要介紹了React使用hook如何實現(xiàn)數(shù)據(jù)雙向綁定方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 使用useImperativeHandle時父組件第一次沒拿到子組件的問題

    使用useImperativeHandle時父組件第一次沒拿到子組件的問題

    這篇文章主要介紹了使用useImperativeHandle時父組件第一次沒拿到子組件的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • React BootStrap用戶體驗框架快速上手

    React BootStrap用戶體驗框架快速上手

    這篇文章主要介紹了React BootStrap用戶體驗框架快速上手的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-03-03
  • React手寫tab切換問題

    React手寫tab切換問題

    今天介紹下React手寫tab切換問題,代碼部分包括父文件,子文件及子文件tab樣式,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2021-11-11
  • react中關(guān)于Context/Provider/Consumer傳參的使用

    react中關(guān)于Context/Provider/Consumer傳參的使用

    這篇文章主要介紹了react中關(guān)于Context/Provider/Consumer傳參的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • React?組件性能最佳優(yōu)化實踐分享

    React?組件性能最佳優(yōu)化實踐分享

    這篇文章主要介紹了React組件性能最佳優(yōu)化實踐分享,React組件性能優(yōu)化的核心是減少渲染真實DOM節(jié)點的頻率,減少Virtual?DOM比對的頻率,更多相關(guān)內(nèi)容需要的朋友可以參考一下
    2022-09-09
  • React中的權(quán)限組件設(shè)計問題小結(jié)

    React中的權(quán)限組件設(shè)計問題小結(jié)

    這篇文章主要介紹了React中的權(quán)限組件設(shè)計,整個過程也是遇到了很多問題,本文主要來做一下此次改造工作的總結(jié),對React權(quán)限組件相關(guān)知識感興趣的朋友一起看看吧
    2022-07-07
  • 在?React?中使用?i18next的示例

    在?React?中使用?i18next的示例

    這篇文章主要介紹了在?React?中使用?i18next,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01

最新評論