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

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

