简体   繁体   English

React Native 和 TypeScript:“RefObject”类型上不存在属性“焦点”<TextInput> &#39;

[英]React Native & TypeScript : Property 'focus' does not exist on type 'RefObject<TextInput>'

That's what I am doing:这就是我正在做的事情:

  declare passCodeInput:  React.RefObject<TextInput>;

But here's an issue that I am getting while trying to call focus method:但这是我在尝试调用focus方法时遇到的一个问题:

setTimeout(() => this.passCodeInput.focus());

在此处输入图像描述

I call focus on current , like, this.passCodeInput.current!.focus()) but this won't work in my case.我将focus称为current ,例如this.passCodeInput.current!.focus()) ,但这在我的情况下不起作用。

You can use ref like below:您可以使用 ref 如下:

import React, { useRef } from 'react'
...


const MyFormComponent = () => {

  const ref_input2 = useRef();
  const ref_input3 = useRef();

  return (
    <>
      <TextInput
        placeholder="Input1"
        autoFocus={true}
        returnKeyType="next"
        onSubmitEditing={() => ref_input2.current.focus()}
      />
      <TextInput
        placeholder="Input2"
        returnKeyType="next"
        onSubmitEditing={() => ref_input3.current.focus()}
        ref={ref_input2}
      />
      <TextInput
        placeholder="Input3"
        ref={ref_input3}
      />
    </>
  )
}

Hope it will help you!希望对您有所帮助!

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

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