【问题标题】:redux mapDispatchToProps with chained arrow functions带有链式箭头函数的 redux mapDispatchToProps
【发布时间】:2018-10-04 20:16:23
【问题描述】:

我能看懂下面的示例代码:

const mapStateToProps = state => {
  return {
    todo: state.todos[0]
  }
}

const mapDispatchToProps = dispatch => {
  return {
    destroyTodo: () =>
      dispatch({
        type: 'DESTROY_TODO'
      })
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(TodoItem)

当在组件中时,我可以调用this.props.destroyTodo(),以便它在函数内执行dispatch(...)

这是根据手册(如果是函数):

mapDispatchToProps:这个参数可以是一个函数,也可以是一个 对象。

如果它是一个函数,它会在组件创建时被调用一次。它 将接收调度作为参数,并应返回一个完整的对象 使用 dispatch 来调度动作的函数。

如果它是一个对象 充满动作创造者,每个动作创造者都将变成一个 调用时自动调度其动作的 prop 函数。 注意:我们建议使用这种“对象速记”形式。

但我很难理解这个现有代码工作链接箭头函数(另一层函数):

export const createBillingRun = id => dispatch => {
    $.ajax({
        type: 'POST',
        url: `/api/billing/billingtypes/${id}/createrun/`,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
    }).done(() => dispatch(pollBillingRuns(id)));
};

我这里已经转换成传统语法了:

export const createBillingRun = function(id) {
    return function(dispatch){
         $.ajax({
            type: 'POST',
            url: `/api/billing/billingtypes/${id}/createrun/`,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
        }).done(() => dispatch(pollBillingRuns(id)));
    }
}

这个函数然后映射到redux connect:

export default connect(
    {...},
    {
        createBillingRun
    },
)(ThePage);

从上面的代码中,createBillingRun返回了一层额外的函数,所以如果我执行createBillingRun(123),它会返回一个接受dispatch作为参数的函数,类似于第一个被传递的例子进入connect。那么谁在执行内部函数呢?

有人可以帮我理解为什么链式箭头函数会起作用吗?

【问题讨论】:

    标签: javascript reactjs redux


    【解决方案1】:

    这仅在您安装了 Redux Thunk 时才有效。它是一个中间件,可以查看您何时返回一个函数、传递该函数调度并调用它。

    https://github.com/reduxjs/redux-thunk

    function createThunkMiddleware(extraArgument) {
      return ({ dispatch, getState }) => next => action => {
        if (typeof action === 'function') {
          return action(dispatch, getState, extraArgument);
        }
    
        return next(action);
      };
    }
    
    const thunk = createThunkMiddleware();
    thunk.withExtraArgument = createThunkMiddleware;
    
    export default thunk;
    

    【讨论】:

    • 现在我可以安息了。
    【解决方案2】:

    当您的 mapDispatchToProps 返回一个对象时,“它被假定为 Redux 操作创建者。” (https://github.com/reduxjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options)

    动作创建者应该返回“纯 JavaScript 对象”的动作。 (https://redux.js.org/basics/actions)

    redux-thunk 提供了从动作创建者返回函数的能力,因此请确保将其作为中间件应用。 (https://github.com/reduxjs/redux-thunk)

    import ReduxThunk from 'redux-thunk'
    
    ...
    
    const createStoreWithMiddleware = applyMiddleware(ReduxThunk)(createStore);
    

    【讨论】:

    • 谢谢,我是一个 redux 菜鸟,在我的一生中,我无法弄清楚它为什么会起作用,但现在它是有意义的。
    • 不用担心。请记住“如果您不确定是否需要它,您可能不需要它。” -github.com/reduxjs/redux-thunk。根据你的例子,我没有看到一个明确的理由,所以请记住这一点:)
    • 这是现有的代码......但我认为只需颠倒箭头链的顺序就可以工作而不需要 thunk lib?
    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2016-03-31
    • 1970-01-01
    • 2016-08-06
    • 2017-10-14
    相关资源
    最近更新 更多