Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

javascript - Is it possible to use Flow and arrow functions with React?

Working on incrementally migrating a project to use Flow types. However, we're getting the following error on any of our arrow functions:

Cannot build a typed interface for this module. You should annotate the exports of this module with types. Missing type annotation at property handleChange:Flow(signature-verification-failure)

With the following syntax, Flow does not complain:

  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 });
  }

However, to avoid doing binding for every helper function we have -- of which there are many in some components -- (& from a previous migration), we currently use arrow functions like the following:

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

With this change, we get the above error. Unsure why it is occurring; is there any workaround for this or way to get flow to stop complaining without FlowIgnores? Thanks!

question from:https://stackoverflow.com/questions/65837845/is-it-possible-to-use-flow-and-arrow-functions-with-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe you want this style:

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

(try)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...