Redux中的Store

  Redux中的actions用来表明发生了什么,Reducers基于action来对state进行更新。那么Store就是action和Reducers之间的桥梁。它的功能如下:
  
- 存储应用的所有state
- 通过其getState()方法获取state
- 通过其dispatch(action)来更新state
- 通过其subscribe(listener)来注册监听器
- 取消subscribe(listener)返回的监听器

Store的创建方式很简单,只要执行createStore(reducers)即可。
下面我们来看下dispatch的主要源码:

  function dispatch(action) {
    try {
      isDispatching = true
      //执行Reducers来更新state
      currentState = currentReducer(currentState, action)
    } finally {
      isDispatching = false
    }
    //触发监听器的执行,依据最新的state来进行处理
    const listeners = currentListeners = nextListeners
    for (let i = 0; i < listeners.length; i++) {
      const listener = listeners[i]
      listener()
    }
    return action
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值