redux的combineReducers源码,中文翻译

本文通过源码解析redux的combineReducers函数,包括检查reducer的初始化状态、处理未知action以及验证reducer结构等关键步骤,帮助理解如何将多个reducer合并成一个单一的reducer。
摘要由CSDN通过智能技术生成

如果看不清楚,可以查看https://segmentfault.com/a/1190000018394259

 

import { ActionTypes } from './createStore'

import isPlainObject from 'lodash/isPlainObject'

import warning from './utils/warning'

/**

* ActionTypes 是这个

* export const ActionTypes = {

* INIT: '@@redux/INIT'

* }

*/

function getUndefinedStateErrorMessage(key, action) { //函数名翻译为获取未定义的state错误信息

         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.`

        )

         //对于action xxx ,reducer yyy 返回undefined

         //一定要很明确的返回之前的state,这样就可以忽略一个action

        //如果你想这个reducer没有返回值,你可以返回null而不是undefined

}

 

//获取与预期不符的state的结构警告信息

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 "` +

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值