【发布时间】:2019-04-22 18:14:31
【问题描述】:
我在定义参考时遇到了一个问题,即
inputRef = React.createRef(null)
//...
const someFunction () => {
if (this.inputRef && this.inputRef.current) {
this.inputRef.current.focus()
}
}
//...
<TextInput ref={inputRef} />
当我访问.focus() 时出现以下错误:
[ts] 类型“从不”上不存在属性“焦点”。 [2339]
我能否以某种方式告诉createRef,此引用可以是null 或TextInput,以便它知道.focus() 可能存在?
【问题讨论】:
标签: reactjs typescript react-native react-ref