如何在React中直接使用Redux
React中使用Redux
開始之前需要強(qiáng)調(diào)一下,redux和react沒(méi)有直接的關(guān)系,你完全可以在React, Angular, Ember, jQuery, or vanilla JavaScript中使用Redux。
盡管這樣說(shuō),redux依然是和React庫(kù)結(jié)合的更好,因?yàn)樗麄兪峭ㄟ^(guò)state函數(shù)來(lái)描述界面的狀態(tài),Redux可以發(fā)射狀態(tài)的更新, 讓他們作出相應(yīng); 目前redux在react中使用是最多的,所以我們需要將之前編寫的redux代碼,融入到react當(dāng)中去。
這里我創(chuàng)建了兩個(gè)組件:
Home組件:其中會(huì)展示當(dāng)前的counter值,并且有一個(gè)+1和+5的按鈕;
Profile組件:其中會(huì)展示當(dāng)前的counter值,并且有一個(gè)-1和-5的按鈕;

首先將結(jié)構(gòu)搭建出來(lái), 然后安裝redux庫(kù): npm i redux
安裝完成后, 安裝我們上一篇文章講解的目錄結(jié)構(gòu), 創(chuàng)建Store文件夾
store/index.js 中創(chuàng)建store
import { createStore } from "redux";
import reducer from "./reducer";
const store = createStore(reducer)
export default storestore/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頁(yè)面中使用store中的state, 核心代碼主要是兩個(gè):
在 componentDidMount 中監(jiān)聽store的變化,當(dāng)數(shù)據(jù)發(fā)生變化時(shí)重新設(shè)置 counter;
在發(fā)生點(diǎn)擊事件時(shí),調(diào)用store的dispatch來(lái)派發(fā)對(duì)應(yīng)的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中監(jiān)聽store的變化,當(dāng)數(shù)據(jù)發(fā)生變化時(shí)重新設(shè)置 counter;
componentDidMount() {
store.subscribe(() => {
const state = store.getState()
this.setState({ counter: state.counter })
})
}
// 核心二: 在發(fā)生點(diǎn)擊事件時(shí),調(diào)用store的dispatch來(lái)派發(fā)對(duì)應(yīng)的action;
changeNum(num) {
store.dispatch(changeNumAction(num))
}
render() {
const { counter } = this.state
return (
<div>
<h2>Home</h2>
<h2>當(dāng)前計(jì)數(shù): {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>當(dāng)前計(jì)數(shù): {counter}</h2>
<button onClick={() => this.changeNum(-1)}>-1</button>
<button onClick={() => this.changeNum(-5)}>-5</button>
</div>
)
}
}
export default Profile
我們發(fā)現(xiàn)Home組件和Profile組件中的代碼是大同小異的, 所以這不是我們最終編寫的代碼, 后面還會(huì)對(duì)代碼進(jìn)行優(yōu)化。
到此這篇關(guān)于在React中直接使用Redux的文章就介紹到這了,更多相關(guān)React使用Redux內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react?card?slider實(shí)現(xiàn)滑動(dòng)卡片教程示例
這篇文章主要為大家介紹了react?card?slider實(shí)現(xiàn)滑動(dòng)卡片教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
React報(bào)錯(cuò)Type '() => JSX.Element[]&apos
這篇文章主要為大家介紹了React報(bào)錯(cuò)Type '() => JSX.Element[]' is not assignable to type FunctionComponent解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
React Native仿美團(tuán)下拉菜單的實(shí)例代碼
本篇文章主要介紹了React Native仿美團(tuán)下拉菜單的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
深入React?18源碼useMemo?useCallback?memo用法及區(qū)別分析
這篇文章主要為大家介紹了React?18源碼深入分析useMemo?useCallback?memo用法及區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
React+Antd 實(shí)現(xiàn)可增刪改表格的示例
這篇文章主要介紹了React+Antd實(shí)現(xiàn)可增刪改表格的示例,幫助大家更好的理解和學(xué)習(xí)使用React,感興趣的朋友可以了解下2021-04-04
react中使用Modal.confirm數(shù)據(jù)不更新的問(wèn)題完美解決方案
這篇文章主要介紹了react中使用Modal.confirm數(shù)據(jù)不更新的問(wèn)題解決方案,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
如何在React?Native開發(fā)中防止滑動(dòng)過(guò)程中的誤觸
在使用React?Native開發(fā)的時(shí),當(dāng)我們快速滑動(dòng)應(yīng)用的時(shí)候,可能會(huì)出現(xiàn)誤觸,導(dǎo)致我們會(huì)點(diǎn)擊到頁(yè)面中的某一些點(diǎn)擊事件,誤觸導(dǎo)致頁(yè)面元素響應(yīng)從而進(jìn)行其他操作,表現(xiàn)出非常不好的用戶體驗(yàn)。2023-05-05
React?component.forceUpdate()強(qiáng)制重新渲染方式
這篇文章主要介紹了React?component.forceUpdate()強(qiáng)制重新渲染方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
使用 React hooks 實(shí)現(xiàn)類所有生命周期
react 自 16.8 開始,引入了 Hooks 概念,使得函數(shù)組件中也可以擁有自己的狀態(tài),并且可以模擬對(duì)應(yīng)的生命周期,這篇文章主要介紹了使用 React hooks 怎么實(shí)現(xiàn)類里面的所有生命周期,需要的朋友可以參考下2023-02-02

