【发布时间】:2019-05-20 16:27:05
【问题描述】:
这行代码
export default memo(LoadingOverlay);
给出流量错误
Missing type annotation for `P`. `P` is a type parameter declared in function type [1] and was implicitly instantiated at call of `memo` [2].Flow(InferError)
还有这一行
export default memo<TProps>(LoadingOverlay);
给出编译时错误。
React memo 和 flow 的正确用法是什么?
编辑:
这是完整的文件示例
// @flow
// React modules
import React, { memo } from 'react';
// Material UI components
import Checkbox from '@material-ui/core/Checkbox';
import FormControlLabel from '@material-ui/core/FormControlLabel';
// Utils and defaults
import './checkbox.scss';
type TProps = {
value: string;
label: string;
checked: boolean;
disabled?: boolean;
onChange: Function;
};
/*
* Presentational component with following props:
* value: String, value as a name of checkbox
* label: String, checkbox label
* checked: Boolean, checkbox state
* disabled: Boolean, checkbox availability state
*/
const Checkbox = (props: TProps) => {
console.log('RENDER CHECKBOX');
const {
value,
label,
checked,
disabled
} = props;
const { onChange } = props;
return (
<FormControlLabel
control={
<Checkbox
checked={checked}
onChange={onChange(value)}
value={value}
disabled={disabled}
color="primary"
/>
}
label={label}
/>
);
};
Checkbox.defaultProps = {
disabled: false,
};
export default memo<TProps>(Checkbox);
【问题讨论】:
-
最后一个应该没问题,你有什么编译错误?
-
Uncaught ReferenceError: TProps is not definedbabel 好像没有去掉这部分。 -
你能说明你是如何定义
TProps的吗?更新问题 -
@Alex 当然,你可以看看。
-
我也有这个问题。似乎@babel/preset-flow 还不支持这种语法 >_>