React@16.x(58)Redux@4.x(7)- 实现 combineReducers
创始人
2025-01-08 12:05:22
0

目录

  • 1,分析
  • 2,实现
    • 2.1,简单实现
    • 2.2,参考源码具体实现
      • 实现

1,分析

大型项目中,数据和操作都比较复杂,所以会对 reducer 进行细分管理。

combineReducers 的作用:结合多个 reducer

举例:

// reducer.js import { combineReducers } from "redux"; import loginReducer from "./login"; import userReducer from "./user";  export default combineReducers({     a: loginReducer,     b: userReducer, }); 
// 使用 import { createStore } from "redux"; import reducer from "./reducer";  const store = createStore(reducer); 

2,实现

2.1,简单实现

// reducer.js import loginReducer from "./login"; import userReducer from "./user";  export default (state = {}, action) => {     const newState = {         a: loginReducer(state.a, action),         b: userReducer(state.b, action),     };     return newState; }; 

解释
createStore(reducer) 初始化时,会调用一次 reducer,此时 state.a = undefined,所以会使用 loginReducer(state = {}) 的默认值进行初始化,并将返回值作为 newState.a 的初始值保存。

之后分发指定 action 时,state.a 就是旧值{},作为 loginReducerstate 传入。

2.2,参考源码具体实现

在使用官方的 combineReducers 时,发现了2个特点:

  • 所有细分的 reducerstate 参数必须指定初始值,如果不想指定,需要默认为 null,而不是 undefined(相当于没有指定初始值)。
  • 所有细分的 reducer 中,不能使用特殊的 type 来判断做一些事情。

举例1:没有初始值,得指定为 null

// reducer/xxx.js export default (state = null, { type, payload }) => {     switch (type) {         case LOGIN:             return payload;         default:             return state;     } }; 

举例2:特殊的 type,比如 createStore() 初始化时的 type=@@redux/INITw.2.n.b.2.o;还有 combineReducers() 初始化时的 type=@@redux/PROBE_UNKNOWN_ACTIONe.a.v.4.3.3

不能在某个 reducer 中使用这2个特殊 type会报错

// reducer/xxx.js export default (state = null, { type, payload }) => {     switch (type) {         case type.startsWith("@@redux/INIT"):             console.log("craeteStore 初始化");         case type.startsWith("@@redux/PROBE_UNKNOWN_ACTION"):             console.log("combineReducers 初始化");         default:             return state;     } }; 

实现

export const combineReducers = (reducers) => {     validate(reducers);     return (state = {}, action) => {         let newState = {};         for (const key in reducers) {             if (Object.hasOwnProperty.call(reducers, key)) {                 const reducer = reducers[key];                 // 参考上面的简单实现。                 newState[key] = reducer(state[key], action);             }         }         return newState;     }; };  function validate(reducers) {     if (typeof reducers !== "object" || Object.getPrototypeOf(reducers) !== Object.prototype) {         throw new Error("reducers 必须是一个 plain Object");     }     for (const key in reducers) {         if (Object.hasOwnProperty.call(reducers, key)) {             const reducer = reducers[key];             // 判断是否进行了初始化,和是否使用了特殊 type             let state = reducer(undefined, { type: `@@redux/INIT${getRandomString()}` });             if (state === undefined) {                 throw new TypeError("reducers must not return undefined");             }             state = reducer(undefined, { type: `@@redux/PROBE_UNKNOWN_ACTION${getRandomString()}` });             if (state === undefined) {                 throw new TypeError("reducers must not return undefined");             }         }     } }  function getRandomString() {     return Math.random().toString(36).substring(2, 8).split("").join("."); } 

以上。

相关内容

热门资讯

绝活儿辅助!广西老友玩老是输怎... 绝活儿辅助!广西老友玩老是输怎么办(辅助挂)都是真的有辅助app(讲解有挂)在进入广西老友玩老是输怎...
法门辅助!福建13水插件(辅助... 法门辅助!福建13水插件(辅助挂)一贯是有辅助技巧(有挂技术)1、许多玩家不知道福建13水插件辅助怎...
办法辅助!潮友会app下载官方... 办法辅助!潮友会app下载官方辅助器(辅助挂)真是真的是有辅助app(有挂教程)该软件可以轻松地帮助...
妙招辅助!邯郸胡乐挂辅助(辅助... 妙招辅助!邯郸胡乐挂辅助(辅助挂)好像存在有辅助插件(有挂方略)1、上手简单,内置详细流程视频教学,...
教程书辅助!乐酷辅助(辅助挂)... 教程书辅助!乐酷辅助(辅助挂)其实存在有辅助脚本(有挂细节)乐酷辅助能透视中分为三种模型:乐酷辅助模...
学习辅助!决战卡五星辅助(辅助... 学习辅助!决战卡五星辅助(辅助挂)本来真的是有辅助软件(有人有挂)学习辅助!决战卡五星辅助(辅助挂)...
绝活辅助!边锋嘉兴麻将辅助器(... 绝活辅助!边锋嘉兴麻将辅助器(辅助挂)真是真的有辅助神器(新版有挂)1、边锋嘉兴麻将辅助器公共底牌简...
举措辅助!枫叶辅助器(辅助挂)... 举措辅助!枫叶辅助器(辅助挂)本来存在有辅助技巧(竟然有挂)1、下载好枫叶辅助器正确养号方法之后点击...
讲义辅助!点我达辅助(辅助挂)... 讲义辅助!点我达辅助(辅助挂)一直存在有辅助技巧(有人有挂)1、点我达辅助辅助器安装包、点我达辅助辅...
模块辅助!威信茶馆有挂的吗(辅... 模块辅助!威信茶馆有挂的吗(辅助挂)一直真的是有辅助脚本(揭秘有挂)1、玩家可以在威信茶馆有挂的吗线...