【问题标题】:Native Base Input Component doesn't support React useRef Hook getting "Cannot read properties of null (reading 'focus')Native Base Input Component 不支持 React useRef Hook getting \"Cannot read properties of null (reading \'focus\')
【发布时间】:2023-02-01 03:04:00
【问题描述】:

    <Input
    ref={nameRef}
    onSubmitEditing={() => emailRef.current.focus()}
    returnKeyType={"next"}
   />

    <Input
    ref={emailRef}
   />


也尝试了 getRef 但出现相同的错误。与来自 ReactNative 的 TextInput 组件一起工作。

使用 NativeBase 版本 3.3.7

【问题讨论】:

    标签: react-native native-base


    【解决方案1】:

    对于 nativeBase 的输入组件,您需要使用 getRef 道具:

    let inputElement = useRef(null);
    
     <Input
      getRef={input => {
        inputElement = input;
      }}
    />
    

    【讨论】:

    • 就像我说的,我也试过 getRef 但没有用。这是因为 ref 返回为 null
    【解决方案2】:

    简单的事情不应该太难,但是当你使用打字稿时,你别无选择。

    文档中也没有。

    但下面是对我有用的。

    const inputRef = useRef<any>()
    
    // input
    <Input onSubmitEditing={()=> inputRef.current.focus() />
    
    <Input ref={ref => inputRef.current = ref } />
    

    ¯_(ツ)_/¯

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 2022-06-17
      • 2022-08-13
      • 2022-12-26
      • 1970-01-01
      • 2021-12-21
      • 2021-12-12
      • 1970-01-01
      • 2022-07-01
      相关资源
      最近更新 更多