【问题标题】:Is it possible to use Flow and arrow functions with React?是否可以在 React 中使用流和箭头函数?
【发布时间】:2021-04-26 11:43:37
【问题描述】:

致力于逐步迁移项目以使用 Flow 类型。但是,我们的任何箭头函数都会出现以下错误:

无法为此模块构建类型化接口。您应该使用类型注释此模块的导出。属性handleChange:Flow(signature-verification-failure) 缺少类型注释

使用以下语法,Flow 不会抱怨:

  constructor(props: ComponentProps) {
      super(props);
      this.state = {
         query: undefined,
      }
      (this: any).handleChange = this.handleChange.bind(this);
  }

  handleChange(event: any) {
    this.setState({ query: event.target.value });
  }

但是,为了避免对我们拥有的每个辅助函数进行绑定(在某些组件中有很多辅助函数)(以及来自以前的迁移),我们目前使用如下箭头函数:

  handleChange = (event: any) => {
    this.setState({ query: event.target.value });
  }

通过此更改,我们得到了上述错误。不确定为什么会发生;在没有 FlowIgnores 的情况下,是否有任何解决方法或让流程停止抱怨的方法?谢谢!

【问题讨论】:

    标签: javascript reactjs flowtype


    【解决方案1】:

    我相信你想要这种风格:

    class Klass {
      member: (string => number) = (num) => num.length;
    }
    

    (try)

    【讨论】:

    • 谢谢!这行得通,忘了因为有一个赋值语句,所以需要在语句的左侧键入函数:)
    猜你喜欢
    • 2017-10-20
    • 2019-08-12
    • 2020-06-30
    相关资源
    最近更新 更多