摘要:是這個(gè)函數(shù)名翻譯為獲取未定義的錯(cuò)誤信息對(duì)于返回一定要很明確的返回之前的這樣就可以忽略一個(gè)如果你想這個(gè)沒(méi)有返回值你可以返回而不是獲取與預(yù)期不符的的結(jié)構(gòu)警告信息聲明結(jié)構(gòu)初始化時(shí)返回如果傳給的是你一定要很明確地返回初始初始可能是如果你不想給這
import { ActionTypes } from "./createStore" import isPlainObject from "lodash/isPlainObject" import warning from "./utils/warning" /** * ActionTypes 是這個(gè) * export const ActionTypes = { * INIT: "@@redux/INIT" * } */ function getUndefinedStateErrorMessage(key, action) { //函數(shù)名翻譯為獲取未定義的state錯(cuò)誤信息 const actionType = action && action.type const actionName = (actionType && `"${actionType.toString()}"`) || "an action" return ( `Given action ${actionName}, reducer "${key}" returned undefined. ` + `To ignore an action, you must explicitly return the previous state. ` + `If you want this reducer to hold no value, you can return null instead of undefined.` ) //對(duì)于action xxx ,reducer yyy 返回undefined //一定要很明確的返回之前的state,這樣就可以忽略一個(gè)action //如果你想這個(gè)reducer沒(méi)有返回值,你可以返回null而不是undefined } //獲取與預(yù)期不符的state的結(jié)構(gòu)警告信息 function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) { const reducerKeys = Object.keys(reducers) const argumentName = action && action.type === ActionTypes.INIT ? "preloadedState argument passed to createStore" : "previous state received by the reducer" if (reducerKeys.length === 0) { return ( "Store does not have a valid reducer. Make sure the argument passed " + "to combineReducers is an object whose values are reducers." ) } if (!isPlainObject(inputState)) { return ( `The ${argumentName} has unexpected type of "` + ({}).toString.call(inputState).match(/s([a-z|A-Z]+)/)[1] + `". Expected argument to be an object with the following ` + `keys: "${reducerKeys.join("", "")}"` ) } const unexpectedKeys = Object.keys(inputState).filter(key => !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key] ) unexpectedKeys.forEach(key => { unexpectedKeyCache[key] = true }) if (unexpectedKeys.length > 0) { return ( `Unexpected ${unexpectedKeys.length > 1 ? "keys" : "key"} ` + `"${unexpectedKeys.join("", "")}" found in ${argumentName}. ` + `Expected to find one of the known reducer keys instead: ` + `"${reducerKeys.join("", "")}". Unexpected keys will be ignored.` ) } } //聲明reducer結(jié)構(gòu) function assertReducerShape(reducers) { Object.keys(reducers).forEach(key => { const reducer = reducers[key] const initialState = reducer(undefined, { type: ActionTypes.INIT }) if (typeof initialState === "undefined") { throw new Error( `Reducer "${key}" returned undefined during initialization. ` + `If the state passed to the reducer is undefined, you must ` + `explicitly return the initial state. The initial state may ` + `not be undefined. If you don"t want to set a value for this reducer, ` + `you can use null instead of undefined.` ) //reducer xxx 初始化時(shí)返回undefined, 如果傳給reducer的state是undefined,你一定要 //很明確地返回初始state, 初始state可能是undefined, 如果你不想給這個(gè)reducer //設(shè)置value值,你可以用null代替undefined } const type = "@@redux/PROBE_UNKNOWN_ACTION_" + Math.random().toString(36).substring(7).split("").join(".") if (typeof reducer(undefined, { type }) === "undefined") { throw new Error( `Reducer "${key}" returned undefined when probed with a random type. ` + `Don"t try to handle ${ActionTypes.INIT} or other actions in "redux/*" ` + `namespace. They are considered private. Instead, you must return the ` + `current state for any unknown actions, unless it is undefined, ` + `in which case you must return the initial state, regardless of the ` + `action type. The initial state may not be undefined, but can be null.` ) //當(dāng)probed(探索)隨機(jī)的type時(shí),reducer xxx 返回undefined. 不要在"redux/*"命名空間操作 // ${ActionTypes.INIT},也就是"@@redux/INIT", 或任意的action.他們是私有的. //相反,對(duì)于未知的action,你應(yīng)該返回當(dāng)前的state,除非它是undefined.不管action的type是什么, //你都應(yīng)該返回初始的state,出示的state可能不是undefined,但可以是null } }) } /** * Turns an object whose values are different reducer functions, into a single * reducer function. It will call every child reducer, and gather their results * into a single state object, whose keys correspond to the keys of the passed * reducer functions. * * 將一個(gè)value值是不同reducer函數(shù)的對(duì)象變成一個(gè)單一的reducer函數(shù),它將會(huì)調(diào)用每一個(gè)子reducer * 將它們的結(jié)果組合成一個(gè)單一的state對(duì)象,這個(gè)對(duì)象的key對(duì)應(yīng)傳進(jìn)來(lái)的reducer的key * * @param {Object} reducers An object whose values correspond to different * reducer functions that need to be combined into one. One handy way to obtain * it is to use ES6 `import * as reducers` syntax. The reducers may never return * undefined for any action. Instead, they should return their initial state * if the state passed to them was undefined, and the current state for any * unrecognized action. * * reducers是一個(gè)對(duì)應(yīng)不同reducer函數(shù)的對(duì)象,這些reducer函數(shù)需要組合成一個(gè)reducer. * 一個(gè)很方便獲取到它的方法就是使用ES6 的`import * as reducers`語(yǔ)法,reducer可能不會(huì) * 返回undefined.相反,它們應(yīng)該返回初始的state. 如果傳給它們的state是undefined,任何 * 不被識(shí)別的action都會(huì)返回當(dāng)前的state * * @returns {Function} A reducer function that invokes every reducer inside the * passed object, and builds a state object with the same shape. * * 返回一個(gè)reducer函數(shù),會(huì)觸發(fā)傳進(jìn)來(lái)的對(duì)象中的每一個(gè)reducer,建立一個(gè)有相同結(jié)構(gòu)的state對(duì)象 */ export default function combineReducers(reducers) { const reducerKeys = Object.keys(reducers) //將reducers的key提取為數(shù)組 const finalReducers = {} for (let i = 0; i < reducerKeys.length; i++) { const key = reducerKeys[i] if (process.env.NODE_ENV !== "production") { if (typeof reducers[key] === "undefined") { warning(`No reducer provided for key "${key}"`) } } if (typeof reducers[key] === "function") { finalReducers[key] = reducers[key] } } const finalReducerKeys = Object.keys(finalReducers) let unexpectedKeyCache if (process.env.NODE_ENV !== "production") { unexpectedKeyCache = {} } let shapeAssertionError try { assertReducerShape(finalReducers) } catch (e) { shapeAssertionError = e } return function combination(state = {}, action) { if (shapeAssertionError) { throw shapeAssertionError } if (process.env.NODE_ENV !== "production") { const warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache) if (warningMessage) { warning(warningMessage) } } let hasChanged = false const nextState = {} for (let i = 0; i < finalReducerKeys.length; i++) { //finalReducerKeys就是reducers的key值復(fù)制了一份 const key = finalReducerKeys[i] //第 i 個(gè)key const reducer = finalReducers[key] //key所對(duì)應(yīng)的reducer const previousStateForKey = state[key] //把key作為屬性賦給state const nextStateForKey = reducer(previousStateForKey, action) //返回新的state if (typeof nextStateForKey === "undefined") { const errorMessage = getUndefinedStateErrorMessage(key, action) throw new Error(errorMessage) } nextState[key] = nextStateForKey //給nextState添加key屬性,并賦值,key與reducer名字相同 hasChanged = hasChanged || nextStateForKey !== previousStateForKey } return hasChanged ? nextState : state } }
源碼解析請(qǐng)參考https://segmentfault.com/a/11...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/102398.html
摘要:只有在你需要實(shí)現(xiàn)代碼分隔,而且需要立即加載一些的時(shí)候才可能會(huì)用到它。 import isPlainObject from lodash/isPlainObject import $$observable from symbol-observable /** * These are private action types reserved by Redux. * For any ...
摘要:在英文中的意思是有效載荷。有一個(gè)動(dòng)作被發(fā)射了顧名思義,替換,這主要是方便開(kāi)發(fā)者調(diào)試用的。相同的輸入必須返回相同的輸出,而且不能對(duì)外產(chǎn)生副作用。怎么辦呢開(kāi)發(fā)者得手動(dòng)維護(hù)一個(gè)訂閱器,才能監(jiān)聽(tīng)到狀態(tài)變化,從而觸發(fā)頁(yè)面重新渲染。 本文是『horseshoe·Redux專(zhuān)題』系列文章之一,后續(xù)會(huì)有更多專(zhuān)題推出來(lái)我的 GitHub repo 閱讀完整的專(zhuān)題文章來(lái)我的 個(gè)人博客 獲得無(wú)與倫比的閱讀體...
摘要:循環(huán)還沒(méi)有結(jié)束,其中的某個(gè)對(duì)進(jìn)行了添加或者刪除,都會(huì)影響到此次循環(huán)的進(jìn)行,帶來(lái)不可預(yù)期的錯(cuò)誤。 首先來(lái)一段 redux 結(jié)合 中間件 thunk、logger 的使用demo 了解一下應(yīng)該如何使用 const redux = require(redux) const { createStore, combineReducers, bindActionCreators, ...
摘要:循環(huán)還沒(méi)有結(jié)束,其中的某個(gè)對(duì)進(jìn)行了添加或者刪除,都會(huì)影響到此次循環(huán)的進(jìn)行,帶來(lái)不可預(yù)期的錯(cuò)誤。 首先來(lái)一段 redux 結(jié)合 中間件 thunk、logger 的使用demo 了解一下應(yīng)該如何使用 const redux = require(redux) const { createStore, combineReducers, bindActionCreators, ...
閱讀 3054·2021-11-22 09:34
閱讀 3646·2021-08-31 09:45
閱讀 3859·2019-08-30 13:57
閱讀 1682·2019-08-29 15:11
閱讀 1687·2019-08-28 18:04
閱讀 3231·2019-08-28 17:59
閱讀 1570·2019-08-26 13:35
閱讀 2195·2019-08-26 10:12