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

问题描述 投票:1回答:1

这是我的调度道具定义:

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,并且dispatch应该返回undefined

我错过了什么?

react-redux flowtype
1个回答
0
投票

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

type DispatchProps = {
  selectRow: (index: number) => SelectRowActionType,
  loadData: (fetchArgs: FetchArgs) => LoadDataActionType,
};
© www.soinside.com 2019 - 2024. All rights reserved.