实现效果,点击显示按钮出现弹出框,点击关闭按钮隐藏弹出框
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> <div style={styles}></div> </div> }
// src/store/const/modal.const.js export const SHOWMODAL = 'showModal' export const HIDEMODAL = 'hideModal' // src/store/actions/modal.actions.js import { SHOWMODAL, HIDEMODAL} from './../const/modal.const' export const show = () => ({type: SHOWMODAL}) export const hide = () => ({type: HIDEMODAL}) // src/store/reducers/counter.reducers.js import { INCREMENT, DECREMENT } from './../const/counter.const' import { SHOWMODAL, HIDEMODAL } from './../const/modal.const' const initialState = { count: 0, // 增加控制modal 显示隐藏显示的状态,默认为隐藏状态 show: false } // eslint-disable-next-line import/no-anonymous-default-export export 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 } }
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> <div style={styles}></div> </div> } // 映射显示英藏状态到props中 const mapStateToProps = state => { return { showState: state.show } } // 把提交actions方法映射到组件props中 const mapDispacthToProps = dispatch => bindActionCreators(modalActions, dispatch) export default connect(mapStateToProps,mapDispacthToProps)(Modal)
通过上面我们发现在点击显示与隐藏之后计数器组件中的数字不见了,因为我们在执行显示与隐藏的方法中并没有返回 计数器的状态所以这个状态丢失掉了,我们需要在更改状态的时候去补上原有的状态
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 } }
这个时候我们的计数器与弹出框组件都已经正常了,但是我们发现reducer函数随着actions动作越来越多变的越来越臃肿,在状态越来越多以后将会变得无法维护
在计数器与弹出框案例中,在reducer函数中,我们既匹配到了计数器案例中的actions,又匹配到了弹出框案例中的actions 这样reducer中的代码将会越来越庞大,越来越臃肿,所以我们接下来拆分reducer,拆分reducer我们需要用到 combineReducers 方法,这个方法要求我们传递一个对象 这个对象是状态对象,返回值是合并后的reducer
import { SHOWMODAL, HIDEMODAL } from './../const/modal.const' const initialState = { show: false } // eslint-disable-next-line import/no-anonymous-default-export export default (state = initialState, action) => { switch (action.type) { case SHOWMODAL: return { ...state, show: true } case HIDEMODAL: return { ...state, show: false } default: return state } }
import { combineReducers } from 'redux' import CounterReducers from './counter.reducers' import ModalReducers from './modal.reducers' // 要求我们传递一个对象 这个对象是状态对象 // 这样写了之后 状态的结构将是这样 counter: { count: 0 } modaler: { show: false } export default combineReducers({ counter: CounterReducers, modaler: ModalReducers })
// src/components/Count.js const mapStateProps = ({ counter }) => ({ count: counter.count, a: '1' }) // src/components/Modal.js const mapStateToProps = ({ modaler }) => { return { showState: modaler.show } }