我今天遇到了一个奇怪的问题,
我有一个使用 flow 键入的 React 组件(也称为 SEO 的 flowtype),由于某种原因,它在此处显示键入错误,这是简化的代码。
type Props = {
someObject: { [string]: string },
};
class ComponentOne extends React.Component<Props> {
render() {
return (
// No idea why there's a linting error, seems like a bug
<div className={this.props.someObject.someKey} />
);
}
}
const WithEnhancements = enhance(magic)(ComponentOne);
export default () => (
<RenderPropComponent>
{({ someProp }) => {
return <WithEnhancements someProp={someProp} />;
}}
</RenderPropComponent>
);
错误是 Prop 验证中缺少“someObject.someKey”
。
我碰壁了,不知道可能是什么原因造成的,其他组件中使用的相同数据结构的相同类型工作绝对正常,这可能是 eslint
不满意的错误多组件文件?
更新:似乎是一个 eslint 错误,出于某种原因,当像这样解构 prop 并使用 someObject.someKey
时它会起作用:
const { someObject } = this.props;
最佳答案
目前这是一个 eslint 错误,已报告 here 。它可以通过像 frontendgirl 提到的那样解构 props 来补救,虽然这看起来可能不是干净的代码,但你只需要为具有 map 对象的组件(具有索引器属性的组件, { [字符串]:字符串}
.
关于javascript - eslint+流: 'attribute' is missing in props validation (Object with identifier),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54671439/