簡(jiǎn)易的redux?createStore手寫實(shí)現(xiàn)示例
1.首先創(chuàng)建一個(gè)store
根目錄創(chuàng)建一個(gè)store文件夾,下面創(chuàng)建一個(gè)index.js

import { createStore } from '../my-redux'
// 書寫reducer函數(shù)
function reducer(state = 0, action) {
switch (action.type) {
case "add":
return state + 1;
case "inc":
return state - 1;
default:
return state;
}
}
// 使用createStore(reducer)創(chuàng)建store對(duì)象并且導(dǎo)出
const state = createStore(reducer);
export default state;
結(jié)合上面的代碼分析
- 創(chuàng)建store是redux庫(kù)的createStore函數(shù)接收一個(gè)reducer函數(shù)進(jìn)行創(chuàng)建。
import { createStore } from '../my-redux'
- 先手寫一個(gè)簡(jiǎn)單的reducer函數(shù)
// 書寫reducer函數(shù)
狀態(tài)值默認(rèn)為0
function reducer(state = 0, action) {
switch (action.type) {
case "add":
return state + 1;
case "inc":
return state - 1;
default:
return state;
}
}
- 將創(chuàng)建的store導(dǎo)出
// 使用createStore(reducer)創(chuàng)建store對(duì)象并且導(dǎo)出 const state = createStore(reducer); export default state;
2.其次創(chuàng)建一個(gè)my-redux

- 將所有的函數(shù)都導(dǎo)入index.js

import createStore from './createStore'
export {
createStore
}
- 創(chuàng)建一個(gè)createStore.js
export default function createStore(reducer) {
let currentState; // 當(dāng)前state值
let currentListeners = []; // store訂閱要執(zhí)行的函數(shù)儲(chǔ)存數(shù)組
// 獲得當(dāng)前state值
function getState() {
return currentState;
}
// 更新state
function dispatch(action) {
// 傳入action 調(diào)用reducer更新state值
currentState = reducer(currentState, action)
// 遍歷調(diào)用訂閱的函數(shù)
currentListeners.forEach((listener) => listener());
}
// 將訂閱事件儲(chǔ)存到currentListeners數(shù)組,并返回unsubscribe 函數(shù)來(lái)取消訂閱
function subscribe(listener) {
currentListeners.push(listener);
// unsubscribe
return () => {
const index = currentListeners.indexOf(listener);
currentListeners.splice(index, 1);
};
}
dispatch({ type: "" }); // 自動(dòng)dispatch一次,保證剛開始的currentState值等于state初始值
// 返回store對(duì)象
return {
getState,
dispatch,
subscribe,
}
}
可以根據(jù)上面給出的代碼步步分析:
①明確createStore接收一個(gè)reducer函數(shù)作為參數(shù)。
②createStore函數(shù)返回的是一個(gè)store對(duì)象,store對(duì)象包含getState,dispatch,subscribe等方法。
- 逐步書寫store上的方法
書寫getState()方法
返回值:當(dāng)前狀態(tài)值
// 獲得當(dāng)前state值
function getState() {
return currentState;
}
書寫dispatch方法
接受參數(shù):action。
dispatch方法,做的事情就是:①調(diào)用reducer函數(shù)更新state。②調(diào)用store訂閱的事件函數(shù)。
currentState是當(dāng)前狀態(tài)值,currentListeners是儲(chǔ)存訂閱事件函數(shù)的數(shù)組。
// 更新state
function dispatch(action) {
// 傳入action 調(diào)用reducer更新state值
currentState = reducer(currentState, action)
// 遍歷調(diào)用訂閱的函數(shù)
currentListeners.forEach((listener) => listener());
}
書寫subscribe方法
接受參數(shù):一個(gè)函數(shù) 返回值:一個(gè)函數(shù),用于取消訂閱unsubscribe
// 將訂閱事件儲(chǔ)存到currentListeners數(shù)組,并返回unsubscribe 函數(shù)來(lái)取消訂閱
function subscribe(listener) {
currentListeners.push(listener);
// unsubscribe
return () => {
const index = currentListeners.indexOf(listener);
currentListeners.splice(index, 1); // 刪除函數(shù)
};
}
返回store對(duì)象
// 返回store對(duì)象
return {
getState,
dispatch,
subscribe,
}
特別注意:
初始進(jìn)入createStore函數(shù)的時(shí)候,需要通過(guò)dipatch方法傳入一個(gè)獨(dú)一無(wú)二的action(reducer函數(shù)默認(rèn)返回state)來(lái)獲取初始的store賦值給currentStore。
可以結(jié)合下面reducer的default項(xiàng)和createStore方法調(diào)用的dispatch來(lái)理解
dispatch({ type: "" }); // 自動(dòng)dispatch一次,保證剛開始的currentState值等于state初始值
// 書寫reducer函數(shù)
function reducer(state = 0, action) {
switch (action.type) {
case "add":
return state + 1;
case "inc":
return state - 1;
default:
return state;
}
}
這樣我們的createStore函數(shù)就已經(jīng)完成了。那接下來(lái)就是檢查我們寫的這玩意是否起作用沒(méi)有了。
3.創(chuàng)建一個(gè)Test組件進(jìn)行檢測(cè)。

老規(guī)矩先拋全部代碼
import React, { Component } from 'react'
import store from './store' // 引入store對(duì)象
export default class Test extends Component {
// 組件掛載之后訂閱forceUpdate函數(shù),進(jìn)行強(qiáng)制更新
componentDidMount() {
this.unsubscribe = store.subscribe(() => {
this.forceUpdate()
})
}
// 組件卸載后取消訂閱
componentWillUnmount() {
this.unsubscribe()
}
// handleclick事件函數(shù)
add = () => {
store.dispatch({ type: 'add' });
console.log(store.getState());
}
inc = () => {
store.dispatch({ type: 'inc' });
console.log(store.getState());
}
render() {
return (
<div>
{/* 獲取store狀態(tài)值 */}
<div>{store.getState()}</div>
<button onClick={this.add}>+</button>
<button onClick={this.inc}>-</button>
</div>
)
}
}
1. 將Test組件記得引入App根組件
import Test from './Test';
function App() {
return (
<div className="App">
<Test />
</div>
);
}
export default App;
2. 將store引入Test組件
import React, { Component } from 'react'
import store from './store' // 引入store對(duì)象
3. 創(chuàng)建一個(gè)類組件,并且使用store.getState()獲得狀態(tài)值
<div>
{/* 獲取store狀態(tài)值 */}
<div>{store.getState()}</div>
<button onClick={this.add}>+</button>
<button onClick={this.inc}>-</button>
</div>
4. 書寫對(duì)應(yīng)的點(diǎn)擊按鈕
// handleclick事件函數(shù)
add = () => {
store.dispatch({ type: 'add' });
console.log(store.getState());
}
inc = () => {
store.dispatch({ type: 'inc' });
console.log(store.getState());
}
<div>
{/* 獲取store狀態(tài)值 */}
<div>{store.getState()}</div>
<button onClick={this.add}>+</button>
<button onClick={this.inc}>-</button>
</div>
這樣是不是就可以實(shí)現(xiàn)了呢?哈哈哈,傻瓜,你是不是猛點(diǎn)鼠標(biāo),數(shù)字還是0呢?
當(dāng)然,這里我們只是更新了store,但是并沒(méi)有更新組件,狀態(tài)的改變可以導(dǎo)致組件更新,但是store又不是Test組件的狀態(tài)。
這里我們使用一個(gè)生命周期函數(shù)componentDidMount和store的訂閱函數(shù)進(jìn)行更新組件的目的,使用componentWillUnmount和store的取消訂閱函數(shù)(訂閱函數(shù)的返回值)。
// 組件掛載之后訂閱forceUpdate函數(shù),進(jìn)行強(qiáng)制更新
componentDidMount() {
this.unsubscribe = store.subscribe(() => {
this.forceUpdate()
})
}
// 組件卸載后取消訂閱
componentWillUnmount() {
this.unsubscribe()
}
好了。這里我們就真實(shí)實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的createStore函數(shù)來(lái)創(chuàng)建store。
以上就是簡(jiǎn)易的redux createStore手寫實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于手寫redux createStore的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
reset.css瀏覽器默認(rèn)樣式表重置(user?agent?stylesheet)的示例代碼
這篇文章主要介紹了reset.css瀏覽器默認(rèn)樣式表重置(user?agent?stylesheet),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
React?Hooks之usePolymerAction抽象代碼結(jié)構(gòu)設(shè)計(jì)理念
這篇文章主要為大家介紹了React?Hooks之usePolymerAction抽象代碼結(jié)構(gòu)設(shè)計(jì)理念,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
react 移動(dòng)端實(shí)現(xiàn)列表左滑刪除的示例代碼
這篇文章主要介紹了react 移動(dòng)端實(shí)現(xiàn)列表左滑刪除的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
React 遠(yuǎn)程動(dòng)態(tài)組件實(shí)踐示例詳解
這篇文章主要為大家介紹了React 遠(yuǎn)程動(dòng)態(tài)組件實(shí)踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
React通過(guò)hook實(shí)現(xiàn)封裝表格常用功能
這篇文章主要為大家詳細(xì)介紹了React通過(guò)hook封裝表格常用功能的使用,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2023-12-12

