javascript - eslint+流: 'attribute' is missing in props validation (Object with identifier)

标签 javascript reactjs eslint flowtype

我今天遇到了一个奇怪的问题,

我有一个使用 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/

相关文章:

meteor - "Resolve error: unable to load resolver "节点 ""

reactjs - 设置 vite --template React 和 eslint (airbnb)

javascript - 使用 casperjs 提交表单 - 新 url 不加载

javascript - 将百分比添加到随机生成的数字

javascript - 为什么这个悬停事件会在页面加载时触发?

css - 使用 Node-SASS 和 CSS 模块的 Create-React-App v2 在本地工作但在 Heroku 中崩溃

javascript - 使用 ReactJS 和 HTML5 Canvas

reactjs - react 应用程序中的localForage : self is undefined

javascript - JSX 中的 eslint "parsing error: Unexpected token {"

javascript - 如何在 asp.net 的客户端不使用任何工具将 div 保存为 PDF?