Redux源码分析之基本概念

Redux源码分析之createStore

Redux源码分析之bindActionCreators

Redux源码分析之combineReducers

Redux源码分析之compose

Redux源码分析之applyMiddleware

bindActionCreators:对disptach的一种封装,可以直接执行或者通过属性方法的调用隐式的调用dispatch,而不用显式调用dispacth

现在我们修改一下代码,引入 acion creater 和 bindActionCreaters,一起来看一下使用效果上有什么不同,重点看红色部分。

let { createStore, bindActionCreators } = self.Redux

//默认state
let todoList = []
// reducer
let todoReducer = function (state = todoList, action) {
switch (action.type) {
case 'add':
return [...state, action.todo]
case 'delete':
return state.filter(todo => todo.id !== action.id)
default:
return state
}
} //创建store
let store = createStore(todoReducer) //订阅
function subscribe1Fn() {
// 输出state
console.log(store.getState())
}
store.subscribe(subscribe1Fn) // action creater
let actionCreaters = {
add: function (todo) { //添加
return {
type: 'add',
todo
}
}, delete: function (id) {
return {
type: 'delete'
,
id
}
}
} let boundActions
= bindActionCreators(actionCreaters, store.dispatch)
boundActions.add({
id: 12,
content: '睡觉觉'
}) let boundAdd = bindActionCreators(actionCreaters.add, store.dispatch)
boundAdd({
id: 13,
content: '陪媳妇'
})

输出结果:

从上面分析 bindActionCreators,两种调用方式,都是对调用的一种封装,不用每次都 dispatch。

  第一

  • bindActionCreators 传入action creater和 dispatch方法
  • 返回一个函数,直接调用就会更新数据,不用显式调用dispatch

     第二

  • bindActionCreators 传入一个对象(属性都是action creater)和 dispatch方法
  • 返回一个对象,直接可以调用属性方法,就会更新数据

我们来看看bindActionCreators.js 源码,

function bindActionCreator(actionCreator, dispatch) {
return (...args) => dispatch(actionCreator(...args))
} export default function bindActionCreators(actionCreators, dispatch) {
if (typeof actionCreators === 'function') {
return bindActionCreator(actionCreators, dispatch)
}
if (typeof actionCreators !== 'object' || actionCreators === null) {
throw new Error(
`bindActionCreators expected an object or a function, instead received ${actionCreators === null ? 'null' : typeof actionCreators}. ` +
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
)
} const keys = Object.keys(actionCreators)
const boundActionCreators = {}
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const actionCreator = actionCreators[key]
if (typeof actionCreator === 'function') {
boundActionCreators[key] = bindActionCreator(actionCreator, dispatch)
}
}
return boundActionCreators
}

bindActionCreators.js 里面有一个 bindActionCreator,bindActionCreators 方法,

bindActionCreators会根据传入的是函数还是对象,采取不同的处理方式,

  • 入参是函数,返回函数,
  • 传入对象,返回对象。

所以重点反而是 bindActionCreator 方法,我们来分解一下bindActionCreator,

  • 返回的是一个函数
  • ...args是动态参数,(rest 参数)
  • actionCreator(...args) 返回一个对象,拿add方法来说,等同于 add(..args)

  那我们来看看  let boundAdd = bindActionCreators(actionCreaters.add, store.dispatch),这个方法返回等同如下 ,那么就简单了,执行boundAdd 就是dispach action

let boundAdd = function(){
dispatch(actionCreators.add(...arguments))
}

参考:

Redux从设计到源码

Redux系列x:源码分析

redux源码解析-redux的架构 - chenby - 博客园

解 redux 源码-知乎

Redux-source-analyze

深入到源码:解读 redux 的设计思路与用法

深入理解redux中间件

Redux源码分析之bindActionCreators的更多相关文章

  1. Redux源码分析之createStore

    接着前面的,我们继续,打开createStore.js, 直接看最后, createStore返回的就是一个带着5个方法的对象. return { dispatch, subscribe, getSt ...

  2. Redux源码分析之applyMiddleware

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  3. Redux源码分析之基本概念

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  4. Redux源码分析之combineReducers

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  5. Redux源码分析之compose

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  6. 史上最全的 Redux 源码分析

    前言 用 React + Redux 已经一段时间了,记得刚开始用Redux 的时候感觉非常绕,总搞不起里面的关系,如果大家用一段时间Redux又看了它的源码话,对你的理解会有很大的帮助.看完后,在回 ...

  7. 正式学习React(四) ----Redux源码分析

    今天看了下Redux的源码,竟然出奇的简单,好吧.简单翻译做下笔记: 喜欢的同学自己可以去github上看:点这里 createStore.js import isPlainObject from ' ...

  8. Redux源码学习笔记

    https://github.com/reduxjs/redux 版本 4.0.0 先了解一下redux是怎么用的,此处摘抄自阮一峰老师的<Redux 入门教程> // Web 应用是一个 ...

  9. redux源码图解:createStore 和 applyMiddleware

    在研究 redux-saga时,发现自己对 redux middleware 不是太了解,因此,便决定先深入解读一下 redux 源码.跟大多数人一样,发现 redux源码 真的很精简,目录结构如下: ...

随机推荐

  1. epoll的ET和LT模式比较 - 源码分析

    eventpoll是一种文件,它实现了一种机制利用一条rdllist队列来避免阻塞地进行poll.eventpoll归根到底还是在使用poll.而ET比LT高效,并不在于是否使用了poll,更不能说是 ...

  2. 自定义分布式RESTful API鉴权机制

    微软利用OAuth2为RESTful API提供了完整的鉴权机制,但是可能微软保姆做的太完整了,在这个机制中指定了数据持久化的方法是用EF,而且对于用户.权限等已经进行了封装,对于系统中已经有了自己的 ...

  3. .Net Mvc Automated Migration 数据迁移

    1.打开程序包管理器控制台 PM> enable-migrations –EnableAutomaticMigration:$true 2.项目工程文件中会生成Migrations文件夹 3.找 ...

  4. v9手机版文章内容不显示

    方法一: 打开PHPCMS v9的/phpcms/templates/default/wap/show.html页面, 将网页中的{$content}替换为:{$rs['content']} 这样wa ...

  5. 无法为具有固定名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6”

    "System.InvalidOperationException"类型的未经处理的异常在 mscorlib.dll 中发生 其他信息: 无法为具有固定名称"MySql. ...

  6. React 在服务端渲染的实现

    原文地址:Server-Side React Rendering 原文作者:Roger Jin 译者:牧云云 React 在服务端渲染的实现 React是最受欢迎的客户端 JavaScript 框架, ...

  7. 怎么用VBS脚本自动注册yy娱乐的账号

    set WshShell=WScript.CreateObject("WScript.Shell") Const user = "hugetech2" Cons ...

  8. OpenShitf 使用教程

    OpenShitf https://www.openshift.com 1.注册openshitf账号 https://www.openshift.com/app/account/new 2.gett ...

  9. 接水问题【NOIP2010普及组】优先队列

    题目描述 学校里有一个水房,水房里一共装有 m 个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为 1. 现在有 n 名同学准备接水,他们的初始接水顺序已经确定.将这些同学按接水顺序从 1到 n ...

  10. mybatis存取blob对象+@Cacheable实现数据缓存

    参考文档: http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 需求场景: 当前业务通过第三方接口查询一个业务数据, ...