使用react+redux實現彈出框案例

2022-10-23 06:01:43

redux 實現彈出框案例

實現效果,點選顯示按鈕出現彈出框,點選關閉按鈕隱藏彈出框

  1. 新建彈出框元件 src/components/Modal.js, 在index.js中引入app元件,在app中去顯示計數器和彈出框元件
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>
}
  1. 彈出框元件顯示隱藏是一個狀態,所以我們儲存到store中,命名為show,因為需要出發action來修改store中的狀態所繫我們需要建立modal.actions.js檔案,來儲存控制顯示隱藏的action,我們還是把顯示與隱藏aciton的type定義為常數方便匯入使用
// 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
    }
}
  1. 彈框的顯示隱藏狀態用display屬性控制所以我們需要把狀態對映到props屬性中,因為show狀態與show顯示方法重名了,所以給show狀態起一個別名,利用 bindActionCreators 方法把 執行 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>
        <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)

通過上面我們發現在點選顯示與隱藏之後計數器元件中的數位不見了,因為我們在執行顯示與隱藏的方法中並沒有返回 計數器的狀態所以這個狀態丟失掉了,我們需要在更改狀態的時候去補上原有的狀態

  1. 補上原有狀態
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 函數

在計數器與彈出框案例中,在reducer函數中,我們既匹配到了計數器案例中的actions,又匹配到了彈出框案例中的actions 這樣reducer中的程式碼將會越來越龐大,越來越臃腫,所以我們接下來拆分reducer,拆分reducer我們需要用到 combineReducers 方法,這個方法要求我們傳遞一個物件 這個物件是狀態物件,返回值是合併後的reducer

  1. 建立 src/store/reducers/modal.reducers.js 檔案,把彈出框的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
    }
}
  1. 建立src/store/reducers/root.reducers.js 檔案,用於合併計數器與彈出框的reducer
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
})
  1. 因為使用 combineReducers 合併reducer的時候改變了state的結構所以我們需要在元件中去更改獲取state的方式
// src/components/Count.js
const mapStateProps = ({ counter }) => ({
    count: counter.count,
    a: '1'
})
// src/components/Modal.js
const mapStateToProps = ({ modaler }) => {
    return {
        showState: modaler.show
    }
}