【发布时间】:2018-10-09 02:41:45
【问题描述】:
从 React 16.3 开始,可以使用 React.createRef() 来访问 DOM 元素。我也在我的项目中使用Flow,但是documentation still uses the old way。
不幸的是,下面的代码失败了:
/* @flow */
import * as React from 'react';
export class TestComponent extends React.Component<{}> {
myRef: React.Ref<HTMLDivElement>
constructor(props: any) {
super(props)
this.myRef = React.createRef()
}
render() {
return (
<div ref={this.myRef} />
)
}
}
出现以下错误:
Cannot instantiate `Ref` because in type argument `ElementType`:
- Either a callable signature is missing in `HTMLDivElement` [1] but exists in
`React.StatelessFunctionalComponent` [2].
- Or `HTMLDivElement` [1] is incompatible with statics of `React.Component` [3].
如何正确输入?
【问题讨论】: