我在定义参考文献时遇到了一个问题,即.
inputRef = React.createRef(null)
//...
const someFunction () => {
if (this.inputRef && this.inputRef.current) {
this.inputRef.current.focus()
}
}
//...
<TextInput ref={inputRef} />
当我访问.focus()
时,会出现以下错误:
[ts]属性"focus"在类型"never"上不存在.[2339]
我能不能告诉createRef
,这个ref可以是null
或TextInput
,所以它知道.focus()
可能存在?