美文网首页工作生活
Redux源码(四) —— bindActionCreators

Redux源码(四) —— bindActionCreators

作者: Xiaobo2020 | 来源:发表于2019-07-02 22:01 被阅读0次

Source Time

function bindActionCreator(actionCreator, dispatch) {
  return function() {
    return dispatch(actionCreator.apply(this, arguments))
  }
}

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
}

Analysis

我们知道,在redux中,action是一个plain object,所以为了方便生成这个action,我们引入了action creator的概念,其实就是一个函数,接受一个参数,返回一个对象,比如下面的addTodo

const addTodo = (text) => {
  return {
    text,
    finished: false,
  };
};

每当我们想添加一个todo的时候只需要调用addTodo并传入名字就行了,但是redux的运行不仅仅止步于此,更关键的是将产生的这个action给触发,也就是dispatch,所以,bindActionCreators就是更进一步,不仅仅是产生action,还实现自动dispatch

核心部分是最开始的五行——函数bindActionCreator

function bindActionCreator(actionCreator, dispatch) {
  return function() {
    return dispatch(actionCreator.apply(this, arguments))
  }
}

创建一个函数,名字叫作bindActionCreator,接受两个参数,一个是actionCreator,也就是提到的addTodo,第二个参数是dispatch,这里的就是store提供的api。而bindActionCreators其实是将接受范围扩大化,不仅仅允许actionCreators是函数,也可以是一个对象,这里不过多赘述。

实际使用往往像是这样的:

const addTodoBindDispatch = bindActionCreators(addTodo, store.dispatch);

PS. 多说一句,感觉这个api并没有什么卵用

All

index
compose
applyMiddleware
bindActionCreators
combineReducers
createStore

相关文章

网友评论

    本文标题:Redux源码(四) —— bindActionCreators

    本文链接:https://www.haomeiwen.com/subject/ycdxhctx.html