【发布时间】:2018-12-30 08:32:36
【问题描述】:
我有以下相当简单的 React 组件。
因为它是一个导航组件,所以我使用withRouter 来访问RouterHistory 对象。
我也在使用 Flow 进行打字,我将 flow-typed 归档为 react-router_v4
// @flow
import * as React from 'react';
import classnames from 'classnames';
import {withRouter} from 'react-router';
import type {RouterHistory} from 'react-router';
import '../sass/Link.scss';
type Props = {
disabled?: boolean,
href: string,
className?: string,
history: RouterHistory,
children: *,
};
const Link = ({history, href, disabled, children, className}: Props) => (
<span
className={classnames(['link', className, {disabled}])}
onClick={() => history.push(href)}>
{children}
</span>
);
Link.defaultProps = {
className: '',
disabled: false,
};
export default withRouter(Link);
当我运行 Flow 检查时,它会产生以下错误:
Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ components/Link.jsx:17:14
Property className is missing in object type [1] but exists in Props [2] in the first argument.
components/Link.jsx
14│ children: *,
15│ };
16│
[2] 17│ const Link = ({history, href, disabled, children, className}: Props) => (
18│ <span
19│ className={classnames(['link', className, {disabled}])}
20│ onClick={() => history.push(href)}>
21│ {children}
22│ </span>
23│ );
24│
25│ Link.defaultProps = {
26│ className: '',
flow-typed/npm/react-router_v4.x.x.js
[1] 120│ Component: React$ComponentType<{| ...ContextRouter, ...P |}>
Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ components/Link.jsx:17:14
Property disabled is missing in object type [1] but exists in Props [2] in the first argument.
components/Link.jsx
14│ children: *,
15│ };
16│
[2] 17│ const Link = ({history, href, disabled, children, className}: Props) => (
18│ <span
19│ className={classnames(['link', className, {disabled}])}
20│ onClick={() => history.push(href)}>
21│ {children}
22│ </span>
23│ );
24│
25│ Link.defaultProps = {
26│ className: '',
flow-typed/npm/react-router_v4.x.x.js
[1] 120│ Component: React$ComponentType<{| ...ContextRouter, ...P |}>
试图破译错误消息,我尝试将导出更改为:
export default withRouter<Props>(Link);
但这只会产生错误:
Cannot call withRouter with Link bound to Component because function [1] is incompatible with statics of
React.Component [2].
我觉得我在这里遗漏了一些东西 - 似乎这里的打字都是光明正大的,但我收到了这些错误。我错过了什么?
【问题讨论】:
标签: javascript reactjs react-router flowtype