简体   繁体   English

React的参考流程类型

[英]Flow type of React's ref

What type returns React.createRef() ? 什么类型返回React.createRef()

I have tried React.Ref<HTMLSelectElement> , React$Ref<HTMLSelectElement> , React$Ref<typeof HTMLSelectElement> and a few more, but nothing works. 我已经尝试过React.Ref<HTMLSelectElement>React$Ref<HTMLSelectElement>React$Ref<typeof HTMLSelectElement>等等,但没有任何效果。 It is either missing, or wrong type. 它要么缺失要么是错误的类型。

Does Flow even support refs created via createRef ? Flow是否支持通过createRef创建的引用?

Judging by the definition of React.createRef , you should be able to do something like this: 根据React.createRef定义来React.createRef ,你应该能够做到这样的事情:

class MyComponent extends React.Component<{}> {
  ref: { current: null | HTMLDivElement };

  constructor(props: any) {
    super(props);
    this.ref = React.createRef();
  }

  render() {
    return <input ref={this.ref} />;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM