實現效果,點選顯示按鈕出現彈出框,點選關閉按鈕隱藏彈出框
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
}
}