- 代表
declare export function connect<-P, -OP, -SP, -DP, S, D>(
// ...
)
- 代表
P
自己的道具. 应该传递给连接组件的道具(即不从状态中派生)。 - 代表
OP
StateProps. 由状态衍生出来的道具,它 必须返回。
SP
- 代表 调度道具. 由调度派生出来的道具,这些道具: mapStateToProps
必须返回。
DP
- 代表 国家. 要通过的状态类型 mapDispatchToProps
.
S
- 代表 调度. 要传递的调度类型 mapStateToProps
.
D
如果上面的说法都是正确的,那么在我看来一切都很清楚,很合理。所以,当我试图传递这些属性的时候。我从flow中得到如下的输出,为什么?mapDispatchToProps
先谢谢你。
/* @flow */
import React from 'react';
import { connect } from 'react-redux';
import * as ActionCreators from './actionCreators';
import type { Todo } from './types';
import type { State } from './reducers';
type OwnProps = {||};
type StateProps = {|
+todos: Array<Todo>;
|};
type DispatchProps = $ReadOnly<typeof ActionCreators>
type Props = {|
...OwnProps;
...StateProps;
...DispatchProps;
|};
export const TodoList = (props: Props) => (
<div>
// ...
</div>
);
const mapStateToProps = (state: State) => ({
todos: state.visibleTodos,
});
const mapDispatchToProps = ActionCreators; // redux will automatically call `bindActionCreators`
export default connect<Props, OwnProps, StateProps, DispatchProps, State, _>(
mapStateToProps,
mapDispatchToProps
)(TodoList);
redux connect函数的flow声明如下:声明导出函数connect( / ... )它取自:https:/github.comflow-typedflow-typed...。
Cannot call connect because:
- Either property todos is missing in object type [1] but exists in StateProps [2] in type argument SP.
- Or property addTodo is missing in object type [3] but exists in DispatchProps [4] in type argument DP.
- Or property todos is missing in object type [5] but exists in StateProps [2] in type argument SP.
- Or property todos is missing in object type [6] but exists in StateProps [2] in type argument SP.
src/components/TodoList/index.jsx
72|
73| const mapDispatchToProps = ActionCreators;
74|
[2][4] 75| export default connect<Props, OwnProps, StateProps, DispatchProps, State, _>(
76| mapStateToProps,
77| mapDispatchToProps,
78| )(TodoList);
flow-typed/npm/react-redux_v7.x.x.js
[1] 148| declare export function connect<-P, -OP, -SP: {||}, -DP: {||}, S, D>(
:
[3] 156| declare export function connect<-P, -OP, -SP, -DP: {||}, S, D>(
:
[5] 166| declare export function connect<-P, -OP, -SP: {||}, -DP, S, D>(
:
[6] 176| declare export function connect<-P, -OP, -SP: {||}, -DP, S, D>(
Found 1 error
原来这就是类型推理的flow bug。我解决了这个问题,明确地添加了
返回类型 StateProps