I have a functional component which causes the following error:
null is not an object (evaluating ‘textInput.current.blur’
I wonder why
import React, { useState, useRef } from "react";
import { TextInput } from "react-native";
const UselessTextInput = ({ hide }) => {
const [text, onChangeText] = React.useState("Useless Text");
const textInput = useRef(null);
const _renderInput = () => {
if (hide) textInput.current.blur();
<TextInput
ref={textInput}
onFocus={() => ...}
onChangeText={onChangeText}
value={text} />
...
return __renderInput();
}
Can anyone explain this behaviour?
>Solution :
use optional chaining to your textInput value
like this : textInput?.current?.blur();