網站首頁 編程語言 正文
本文實例為大家分享了用react+redux實現彈出框案例的具體代碼,供大家參考,具體內容如下
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>
}
2、彈出框組件顯示隱藏是一個狀態,所以我們存儲到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
? ? }
}
3、彈框的顯示隱藏狀態用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)
通過上面我們發現在點擊顯示與隱藏之后計數器組件中的數字不見了,因為我們在執行顯示與隱藏的方法中并沒有返回 計數器的狀態所以這個狀態丟失掉了,我們需要在更改狀態的時候去補上原有的狀態
4、補上原有狀態
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
? ? }
}
2、創建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
})
3、因為使用 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
? ? }
}
原文鏈接:https://blog.csdn.net/qq_36223186/article/details/117449158
相關推薦
- 2022-02-10 Linux環境下安裝docker環境(親測無坑)_docker
- 2023-01-26 Redis實戰之Jedis使用技巧詳解_Redis
- 2022-03-07 golang強制類型轉換和類型斷言_Golang
- 2022-07-02 Prometheus+Grafana監控Docker容器和Linux主機
- 2022-10-15 淺談React?useDebounce?防抖原理_React
- 2022-04-19 asp.core?同時兼容JWT身份驗證和Cookies?身份驗證兩種模式(示例詳解)_實用技巧
- 2022-04-05 Pandas的DataFrame如何做交集,并集,差集與對稱差集_python
- 2022-07-26 對Python中GIL(全局解釋器鎖)的一點理解淺析_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同步修改后的遠程分支