使用bindActionCreators进行流式输入不适用于mapDispatchToProps

时间:2018-03-24 00:12:52

标签: react-redux flowtype

这是我的调度道具定义:

type DispatchProps = {
  selectRow: (index: number) => void,
  loadData: (fetchArgs: FetchArgs) => void,
};

// This works
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
  selectRow: (selectedRowIndex: number) => dispatch(actions.selectRow(selectedRowIndex)),
  loadData: (fetchArgs: FetchArgs) => dispatch(actions.loadData(fetchArgs)),
});

// This doesn't work
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => bindActionCreators({
  selectRow: actions.selectRow,
  loadData: actions.loadData,
}, dispatch);

Flow抱怨说:

SelectRowAction [1] is incompatible with undefined [2] in the return value of property `selectRow`.

如果bindActionCreator类型定义试图返回动作创建者返回值(在本例中为SelectRowAction),但这并没有任何意义,因为那些值传递给dispatch,调度应该返回undefined

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,您需要在DispatchProps中定义函数的返回类型,而不是void 例如:

type DispatchProps = {
  selectRow: (index: number) => SelectRowActionType,
  loadData: (fetchArgs: FetchArgs) => LoadDataActionType,
};