最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年07月27日 15:19:56   作者:敢問(wèn)  
本文主要介紹了react-redux action傳參及多個(gè)state處理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(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  第一個(gè)參數(shù)  state  第二個(gè)參數(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
    }
  }

多個(gè)state狀態(tài)

增加一個(gè)新的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}>多個(gè) 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  第一個(gè)參數(shù)  state  第二個(gè)參數(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}>多個(gè) 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

多個(gè)reducer進(jìn)行整合   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  第一個(gè)參數(shù)  state  第二個(gè)參數(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  第一個(gè)參數(shù)  state  第二個(gè)參數(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é)果是一個(gè)對(duì)象

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

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

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

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

import { colorstate } from "../state/state";
//2.定義 reducer  第一個(gè)參數(shù)  state  第二個(gè)參數(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  第一個(gè)參數(shù)  state  第二個(gè)參數(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傳參及多個(gè)state處理的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)react-redux action傳參及多個(gè)state處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何使用 React Native WebView 實(shí)現(xiàn) App 與 Web 的通訊

    如何使用 React Native WebView 實(shí)現(xiàn) App&nb

    通過(guò) react-native-webview,我們可以輕松實(shí)現(xiàn) App 與 Web 的雙向通訊,這種技術(shù)非常適合需要在移動(dòng)應(yīng)用中嵌入復(fù)雜網(wǎng)頁(yè)功能的場(chǎng)景,感興趣的朋友一起看看吧
    2024-12-12
  • react實(shí)現(xiàn)antd線上主題動(dòng)態(tài)切換功能

    react實(shí)現(xiàn)antd線上主題動(dòng)態(tài)切換功能

    這篇文章主要介紹了react實(shí)現(xiàn)antd線上主題動(dòng)態(tài)切換功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • React Hook的使用示例

    React Hook的使用示例

    這篇文章主要介紹了React Hook的使用示例,幫助大家更好的理解和學(xué)習(xí)使用React,感興趣的朋友可以了解下
    2021-04-04
  • react配合antd組件實(shí)現(xiàn)的管理系統(tǒng)示例代碼

    react配合antd組件實(shí)現(xiàn)的管理系統(tǒng)示例代碼

    這篇文章主要介紹了react配合antd組件實(shí)現(xiàn)的管理系統(tǒng)示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • React-Native使用Mobx實(shí)現(xiàn)購(gòu)物車功能

    React-Native使用Mobx實(shí)現(xiàn)購(gòu)物車功能

    本篇文章主要介紹了React-Native使用Mobx實(shí)現(xiàn)購(gòu)物車功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • react中的useContext具體實(shí)現(xiàn)

    react中的useContext具體實(shí)現(xiàn)

    useContext是React提供的一個(gè)鉤子函數(shù),用于在函數(shù)組件中訪問(wèn)和使用Context,useContext的實(shí)現(xiàn)原理涉及React內(nèi)部的機(jī)制,本文給大家介紹react中的useContext具體實(shí)現(xiàn),感興趣的朋友一起看看吧
    2023-11-11
  • React Router 如何使用history跳轉(zhuǎn)的實(shí)現(xiàn)

    React Router 如何使用history跳轉(zhuǎn)的實(shí)現(xiàn)

    這篇文章主要介紹了React Router 如何使用history跳轉(zhuǎn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • React封裝CustomSelect組件思路詳解

    React封裝CustomSelect組件思路詳解

    小編需要封裝一個(gè)通過(guò)Popover彈出框里可以自定義渲染內(nèi)容的組件,渲染內(nèi)容暫時(shí)有: 單選框, 復(fù)選框,接下來(lái)通過(guò)本文給大家分享React封裝CustomSelect組件思路,需要的朋友可以參考下
    2022-07-07
  • React開(kāi)啟代理的2種實(shí)用方式

    React開(kāi)啟代理的2種實(shí)用方式

    最近有不少伙伴詢問(wèn)react的代理配置,自己也去試驗(yàn)了一下發(fā)現(xiàn)不少的問(wèn)題,在這就將所遇到的心得分享出來(lái),這篇文章主要給大家介紹了關(guān)于React開(kāi)啟代理的2種實(shí)用方式的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • 淺談React組件props默認(rèn)值的設(shè)置

    淺談React組件props默認(rèn)值的設(shè)置

    本文主要介紹了淺談React組件props默認(rèn)值的設(shè)置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評(píng)論

扶沟县| 蒙阴县| 沁水县| 凤山市| 义乌市| 洪泽县| 汽车| 宝清县| 古蔺县| 武鸣县| 平陆县| 郧西县| 平罗县| 汤原县| 台北市| 襄垣县| 疏附县| 清涧县| 中牟县| 尼玛县| 莱州市| 涞源县| 大关县| 寿阳县| 灵石县| 澄迈县| 仁化县| 英山县| 黄梅县| 临海市| 久治县| 通化县| 六安市| 雅安市| 呼图壁县| 行唐县| 利津县| 镇远县| 新疆| 芜湖市| 罗定市|