I came across an issue when defining refs i.e.
inputRef = React.createRef(null)
//...
const someFunction () => {
if (this.inputRef && this.inputRef.current) {
this.inputRef.current.focus()
}
}
//...
<TextInput ref={inputRef} />
When I access .focus()
I get following error:
[ts] Property 'focus' does not exist on type 'never'. [2339]
Can I somehow tell createRef
that this ref can be null
or TextInput
so it knows that .focus()
may exist?
ref attribute on TextInput is used to store a reference to a DOM node. React will call the ref callback with the DOM element when the component mounts, and call it with null when it unmounts.
You can try the following:
inputRef = React.createRef<TextInput>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With