【问题标题】:How to get focus from one textinput to another textinput?如何将焦点从一个文本输入转移到另一个文本输入?
【发布时间】:2020-09-26 14:31:58
【问题描述】:

我创建了一个自定义文本输入,假设它是 现在我想让用户从一个文本输入集中到另一个文本输入。让它从

let {login,pass}=useRef(null)
<MyTextInput placeholder'login' ref={login} onSubmitEditing={()=>pass.focus()}/> 
<MyTextInput placeholder='password' ref={pass}/>

现在我正在使用 react native 中的功能组件并且我已经尝试将 ref 添加到它上面。但我收到错误,因为无法读取未定义的属性焦点。

谁能帮我用这种文本输入添加焦点?

【问题讨论】:

  • 你能试试这个答案吗?我想这会回答你的问题! Focus Next Input
  • 谢谢@Veno 答案是类组件。我想让它与功能组件一起工作。

标签: reactjs react-native react-hooks


【解决方案1】:

假设您的自定义 MyTextInput 支持与 TextInput 相同的道具,这是一个简单的 sn-p 应该适用于您的情况

export default function App() {
  const useranme = React.useRef(null)
  const password = React.useRef(null)
  return (
    <View>
      <MyTextInput onSubmitEditing={()=>password.current.focus()} ref={useranme} />
      <MyTextInput ref={password} />
    </View>
  );
}

【讨论】:

  • 感谢@SaachiTech,但我收到错误,因为无法读取 null 的属性焦点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 2010-12-20
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多