代码之家  ›  专栏  ›  技术社区  ›  Ilja

为react本机文本输入创建引用时出现的类型脚本问题

  •  2
  • Ilja  · 技术社区  · 7 年前

    我在定义参考文献时遇到了一个问题,即

    inputRef = React.createRef(null)
    
    //...
    
    const someFunction () => {
     if (this.inputRef && this.inputRef.current) {
       this.inputRef.current.focus()
     }
    }
    
    //...
    
    <TextInput ref={inputRef} />
    

    我进入的地方 .focus() 我得到以下错误

    类型“never”上不存在属性“focus”。〔2339〕

    我能告诉你吗 createRef 这个引用可以是null或textinput,所以它知道 焦点() 它能存在吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Peter Ambruzs    7 年前

    您可以尝试以下操作:

    inputRef = React.createRef<TextInput>();
    
    推荐文章