redux 實(shí)現(xiàn)彈出框案例實(shí)現(xiàn)效果,點(diǎn)擊顯示按鈕出現(xiàn)彈出框,點(diǎn)擊關(guān)閉按鈕隱藏彈出框
- 新建彈出框組件 src/components/Modal.js, 在index.js中引入app組件,在app中去顯示計(jì)數(shù)器和彈出框組件
function Modal ({ showState, show, hide }) {const styles = {width: 200,height: 200,position: 'absolute',top: '50%',left: '50%',marginTop: -100,marginLeft: -100,backgroundColor: 'skyblue',}return <div><button>顯示</button><button>隱藏</button><divstyle={styles}></div></div>}- 彈出框組件顯示隱藏是一個(gè)狀態(tài),所以我們存儲(chǔ)到store中,命名為show,因?yàn)樾枰霭l(fā)action來修改store中的狀態(tài)所系我們需要?jiǎng)?chuàng)建modal.actions.js文件,來存儲(chǔ)控制顯示隱藏的action,我們還是把顯示與隱藏aciton的type定義為常量方便導(dǎo)入使用
// src/store/const/modal.const.jsexport const SHOWMODAL = 'showModal'export const HIDEMODAL = 'hideModal'// src/store/actions/modal.actions.jsimport { SHOWMODAL, HIDEMODAL} from './../const/modal.const'export const show = () => ({type: SHOWMODAL})export const hide = () => ({type: HIDEMODAL})// src/store/reducers/counter.reducers.jsimport { INCREMENT, DECREMENT } from './../const/counter.const'import { SHOWMODAL, HIDEMODAL } from './../const/modal.const'const initialState = {count: 0,// 增加控制modal 顯示隱藏顯示的狀態(tài),默認(rèn)為隱藏狀態(tài)show: false}// eslint-disable-next-line import/no-anonymous-default-exportexport default (state = initialState, action) => {switch (action.type) {case INCREMENT:return {count: state.count + action.payload}case DECREMENT:return {count: state.count - action.payload}case SHOWMODAL:return {show: true}case HIDEMODAL:return {show: false}default:return state}}- 彈框的顯示隱藏狀態(tài)用display屬性控制所以我們需要把狀態(tài)映射到props屬性中,因?yàn)閟how狀態(tài)與show顯示方法重名了,所以給show狀態(tài)起一個(gè)別名,利用 bindActionCreators 方法把 執(zhí)行 dispatch 提交actions的方法映射到props中
import React from 'react'import { connect } from 'react-redux'import * as modalActions from './../store/actions/modal.actions'import { bindActionCreators } from 'redux'function Modal ({ showState, show, hide }) {const styles = {width: 200,height: 200,position: 'absolute',top: '50%',left: '50%',marginTop: -100,marginLeft: -100,backgroundColor: 'skyblue',// 增加控制顯示隱藏的css樣式display: showState ? 'block' : 'none'}return <div><button onClick={show}>顯示</button><button onClick={hide}>隱藏</button><divstyle={styles}></div></div>}// 映射顯示英藏狀態(tài)到props中const mapStateToProps = state => {return {showState: state.show}}// 把提交actions方法映射到組件props中const mapDispacthToProps = dispatch => bindActionCreators(modalActions, dispatch)export default connect(mapStateToProps,mapDispacthToProps)(Modal)通過上面我們發(fā)現(xiàn)在點(diǎn)擊顯示與隱藏之后計(jì)數(shù)器組件中的數(shù)字不見了,因?yàn)槲覀冊(cè)趫?zhí)行顯示與隱藏的方法中并沒有返回 計(jì)數(shù)器的狀態(tài)所以這個(gè)狀態(tài)丟失掉了,我們需要在更改狀態(tài)的時(shí)候去補(bǔ)上原有的狀態(tài)- 補(bǔ)上原有狀態(tài)
export default (state = initialState, action) => {switch (action.type) {case INCREMENT:return {...state,count: state.count + action.payload}case DECREMENT:return {...state,count: state.count - action.payload}case SHOWMODAL:return {...state,show: true}case HIDEMODAL:return {...state,show: false}default:return state}}這個(gè)時(shí)候我們的計(jì)數(shù)器與彈出框組件都已經(jīng)正常了,但是我們發(fā)現(xiàn)reducer函數(shù)隨著actions動(dòng)作越來越多變的越來越臃腫,在狀態(tài)越來越多以后將會(huì)變得無法維護(hù)拆分reducer 函數(shù)在計(jì)數(shù)器與彈出框案例中,在reducer函數(shù)中,我們既匹配到了計(jì)數(shù)器案例中的actions,又匹配到了彈出框案例中的actions 這樣reducer中的代碼將會(huì)越來越龐大,越來越臃腫,所以我們接下來拆分reducer,拆分reducer我們需要用到 combineReducers 方法,這個(gè)方法要求我們傳遞一個(gè)對(duì)象 這個(gè)對(duì)象是狀態(tài)對(duì)象,返回值是合并后的reducer
經(jīng)驗(yàn)總結(jié)擴(kuò)展閱讀
- 珂潤(rùn)面霜適合油皮使用嗎?
- 鑄工膠使用范圍
- 你真的會(huì)使用Typora嗎?
- 除菌液怎么使用
- 西班牙安瓶使用方法是什么?
- 豬油膏和隔離使用順序是怎么樣的?
- vue3中$attrs的變化與inheritAttrs的使用
- win10本地python第三方庫(kù)安裝成功,但是pycharm項(xiàng)目無法使用解決方案
- Java實(shí)現(xiàn)7種常見密碼算法
- 華為快充66w和40w有什么區(qū)別-使用對(duì)比
