Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何将流与React.createRef()一起使用?_Reactjs_Flowtype - Fatal编程技术网

Reactjs 如何将流与React.createRef()一起使用?

Reactjs 如何将流与React.createRef()一起使用?,reactjs,flowtype,Reactjs,Flowtype,自React 16.3以来,可以使用访问DOM元素。我也在我的项目中使用,但是 以下代码不幸失败: /*@flow*/ 从“React”导入*作为React; 导出类TestComponent扩展了React.Component{ myRef:React.Ref 构造器(道具:任何){ 超级(道具) this.myRef=React.createRef() } render(){ 返回( ) } } 出现以下错误: Cannot instantiate `Ref` because in typ

自React 16.3以来,可以使用访问DOM元素。我也在我的项目中使用,但是

以下代码不幸失败:

/*@flow*/
从“React”导入*作为React;
导出类TestComponent扩展了React.Component{
myRef:React.Ref
构造器(道具:任何){
超级(道具)
this.myRef=React.createRef()
}
render(){
返回(
)
}
}
出现以下错误:

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].
如何正确键入?

有一个相关的问题

如果尚未修复,您可以自己键入:

type RefObject = {|
  current: any,
|};

这是内部输入的方式。

查看以下的流类型定义:

声明导出函数createRef(
):{current:null | React$ElementRef};
我可以做这样的事情:

/*@flow*/
从“React”导入*作为React;
导出类TestComponent扩展了React.Component{
myRef:{current:null | htmldevelment}
构造器(道具:任何){
超级(道具)
this.myRef=React.createRef()
}
render(){
返回(
)
}
}
如果您使用的是“类属性”,您可以通过以下方式
createRef()

// @flow
import * as React from 'react';

export class TestComponent extends React.Component<{}> {
  myRef = React.createRef<HTMLElement>();

  render() {
    return (
      <div ref={this.myRef} />
    )
  }
}
/@flow
从“React”导入*作为React;
导出类TestComponent扩展了React.Component{
myRef=React.createRef();
render(){
返回(
)
}
}

在我的例子中,我使用的是formik引用

// @flow
import React from 'react';
import { Formik } from "formik"


export class TestComponent extends React.Component<Props> {
   formikRef = React.createRef<Formik>();

  render() {
     return (
         <Formik 
            ...,
            ref={this.formikRef}
         />
     )
  }
}
/@flow
从“React”导入React;
从“Formik”导入{Formik}
导出类TestComponent扩展了React.Component{
formikRef=React.createRef();
render(){
返回(
)
}
}

当尝试使用自定义的重新对象时,我遇到错误
无法将React.createRef()分配给this.myRef(),因为不精确的对象类型[1]与精确的重新对象[2]不兼容。
知道吗?(我对flow很陌生)。@Zardoz尝试这样:
typereObject={current:any}