網站首頁 編程語言 正文
action 中傳遞參數
App.js 中 傳遞自己的參數
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 第一個參數 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);
?整合 reducer? ??combineReducers(reducers)
redux/combineReducers.md at master · reduxjs/redux · GitHub
多個reducer進行整合? ?reducer下創建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 第一個參數 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,?
映射的時候需要解構
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 } }
原文鏈接:https://blog.csdn.net/weixin_41040445/article/details/125906957
相關推薦
- 2022-07-14 python輸出小數精度控制的方法_python
- 2022-12-06 React.memo?和?useMemo?的使用問題小結_React
- 2022-11-25 Vmware臨時文件存放路徑_VMware
- 2022-07-09 apt報錯Hash 校驗和不符解決辦法
- 2022-03-03 gyp ERR! configure error. gyp ERR! stack Error: gy
- 2022-04-05 C#反射調用拓展類方法實例代碼_C#教程
- 2022-11-19 GoLang中的iface?和?eface?的區別解析_Golang
- 2022-09-09 Python利用Turtle繪畫簡單圖形_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支