最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How should an onChange event be annotated in an exported React Component? - Stack Overflow

programmeradmin0浏览0评论

I have a React Component with an onChange handler:

// @flow
import React, {Component} from 'react';

export default class MyList extends Component {
  handleChange = (event) => {
    // Do something with event.target.value
    // which will be the value typed in the input field.
  }

  render() {
    return (
      <input type="text" onChange={this.handleChange}> />
    );
  }
}

and Flow plains about this because it is an exported class: Parameter `event` missing annotation

How can I annotate the event parameter in the handleChange function? As far as I know, this event is generated at the JavaScript level and doesn't have any Flow typing.

Alternatively, can Flow be configured to not display these "missing annotation" errors?

I have a React Component with an onChange handler:

// @flow
import React, {Component} from 'react';

export default class MyList extends Component {
  handleChange = (event) => {
    // Do something with event.target.value
    // which will be the value typed in the input field.
  }

  render() {
    return (
      <input type="text" onChange={this.handleChange}> />
    );
  }
}

and Flow plains about this because it is an exported class: Parameter `event` missing annotation

How can I annotate the event parameter in the handleChange function? As far as I know, this event is generated at the JavaScript level and doesn't have any Flow typing.

Alternatively, can Flow be configured to not display these "missing annotation" errors?

Share Improve this question asked Nov 8, 2016 at 18:51 Ryan H.Ryan H. 7,8644 gold badges41 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I found another solution, using synthetic events: something like the following

onChange (event: SyntheticInputEvent<EventTarget>): void {
  this.setState({ text: event.target.value })
}

You can find the typings here https://github./facebook/flow/blob/master/lib/dom.js

handleChange = (event: Event) => {
  if (event.target instanceof HTMLInputElement) {
    console.log(event.target.value)
  }
}
发布评论

评论列表(0)

  1. 暂无评论