網站首頁 編程語言 正文
React中使用Redux
開始之前需要強調一下,redux和react沒有直接的關系,你完全可以在React, Angular, Ember, jQuery, or vanilla JavaScript中使用Redux。
盡管這樣說,redux依然是和React庫結合的更好,因為他們是通過state函數來描述界面的狀態,Redux可以發射狀態的更新, 讓他們作出相應; 目前redux在react中使用是最多的,所以我們需要將之前編寫的redux代碼,融入到react當中去。
這里我創建了兩個組件:
Home組件:其中會展示當前的counter值,并且有一個+1和+5的按鈕;
Profile組件:其中會展示當前的counter值,并且有一個-1和-5的按鈕;
首先將結構搭建出來, 然后安裝redux庫: npm i redux
安裝完成后, 安裝我們上一篇文章講解的目錄結構, 創建Store文件夾
store/index.js 中創建store
import { createStore } from "redux"; import reducer from "./reducer"; const store = createStore(reducer) export default store
store/constants.js 中定義變量
export const CHANGE_NUM = "change_num"
store/reducer.js
import { CHANGE_NUM } from "./constants" const initialState = { counter: 10 } export default function reducer(state = initialState, action) { switch(action.type) { case CHANGE_NUM: return {...state, counter: state.counter + action.num} default: return state } }
store/actionCreators.js
import { CHANGE_NUM } from "./constants" export const changeNumAction = (num) => ({ type: CHANGE_NUM, num })
store中編寫完成后, 在Home和Profile頁面中使用store中的state, 核心代碼主要是兩個:
在 componentDidMount 中監聽store的變化,當數據發生變化時重新設置 counter;
在發生點擊事件時,調用store的dispatch來派發對應的action;
Home組件
import React, { PureComponent } from 'react' import store from '../store' import { changeNumAction } from '../store/actionCreators' export class Home extends PureComponent { constructor() { super() this.state = { counter: store.getState().counter } } // 核心一: 在componentDidMount中監聽store的變化,當數據發生變化時重新設置 counter; componentDidMount() { store.subscribe(() => { const state = store.getState() this.setState({ counter: state.counter }) }) } // 核心二: 在發生點擊事件時,調用store的dispatch來派發對應的action; changeNum(num) { store.dispatch(changeNumAction(num)) } render() { const { counter } = this.state return ( <div> <h2>Home</h2> <h2>當前計數: {counter} </h2> <button onClick={() => this.changeNum(1)}>+1</button> <button onClick={() => this.changeNum(5)}>+5</button> </div> ) } } export default Home
Profile組件
import React, { PureComponent } from 'react' import store from '../store' import { changeNumAction } from '../store/actionCreators' export class Profile extends PureComponent { constructor() { super() this.state = { counter: store.getState().counter } } componentDidMount() { store.subscribe(() => { const state = store.getState() this.setState({ counter: state.counter }) }) } changeNum(num) { store.dispatch(changeNumAction(num)) } render() { const { counter } = this.state return ( <div> <h2>Profile</h2> <h2>當前計數: {counter}</h2> <button onClick={() => this.changeNum(-1)}>-1</button> <button onClick={() => this.changeNum(-5)}>-5</button> </div> ) } } export default Profile
我們發現Home組件和Profile組件中的代碼是大同小異的, 所以這不是我們最終編寫的代碼, 后面還會對代碼進行優化。
原文鏈接:https://blog.csdn.net/m0_71485750/article/details/126747191
相關推薦
- 2022-07-31 C++超詳細講解引用和指針_C 語言
- 2022-07-29 linux目錄管理方法介紹_linux shell
- 2022-12-05 Linux系統查看服務器帶寬及網絡使用情況的具體方法_服務器其它
- 2022-05-13 eigen交叉編譯
- 2023-07-25 node項目使用crypto模塊為用戶密碼加密
- 2022-08-31 Linux環境下安裝python3_python
- 2022-10-29 css屬性選擇器*=與~=的區別
- 2022-04-04 微信登陸失敗Error: invalid code
- 最近更新
-
- 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同步修改后的遠程分支