zoukankan      html  css  js  c++  java
  • Redux 源码解读--createStore,js

    一、依赖:$$observable、ActionTypes、isPlainObject

    二、接下来看到直接 export default 一个 createStore 函数,下面根据代码以及注释来分析这个函数

    1、函数本身

      注释大致意思解读:

      这个函数用来创建一个保存 state tree (状态树) 的 Redux store。

      你唯一能修改 store 里面的 data 数据的方式是通过调用 dispatch() 方法。

      在你的APP中只能存在一个 store。

      如果要指定 state tree 的不同部分如何相应 actions 的操作,你可以用 combineReducers 方法来合并几个 reucer 到一个 reducer 函数中。

    2、参数:reducer、preloadedState、enhancer

      reducer :function 类型。一个传入 current state (当前state) 和 要处理的 action ,返回新的 state tree 的函数。

      preloadedState:可以是任何类型。初始的state。如果你使用 combineReducers 去生产根 reducer 函数,它必须是一个与 combineReducer 的键相同的对象。

      enhancer:function 类型。store 的增强器。你可以选择指定它来增加具有第三方第三方能力的 store。唯一一个跟 Redux 一起使用的 enhancer 是 applyMiddleware()。

    3、返回值 store

      一个可以读取 state、dispach(调度) actions 和 subscribe(监听)变化 的 Redux store。

    4、函数体

      1)、刚开始就是三个 if 判断

        第一个判断,如果传入的 preloadState 是一个函数,并且 enhancer 是 undefined的,那么就认为用户第二个参数传入的是 enhancer 而不是 preloadState,进行相应的赋值操作。

        第二个判断,如果 enhancer 存在了,但是它不是一个 function,那么就报错:enhancer 必须是一个函数。如果判断是一个函数,则return enhancer(createStore)(reducer, preloadState),执行中间件的函数,至此,该文件下面的内容就不执行了。

        第三个判断,如果 reducer 不是 function 类型的,则报错:希望 reducer 是一个函数。

  • 相关阅读:
    HDU 1022 Train Problem I
    HDU 1702 ACboy needs your help again!
    HDU 1294 Rooted Trees Problem
    HDU 1027 Ignatius and the Princess II
    HDU 3398 String
    HDU 1709 The Balance
    HDU 2152 Fruit
    HDU 1398 Square Coins
    HDU 3571 N-dimensional Sphere
    HDU 2451 Simple Addition Expression
  • 原文地址:https://www.cnblogs.com/z-one/p/9056897.html
Copyright © 2011-2022 走看看