Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

294
Visualizações
Cannot useRef() in functional component. null is not an object

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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you set a ref to a DOM node using the ref attribute, the ref's current property will have that node only after it's set i.e. in useEffect callbacks or from next renders.

function Counter() {
  const [count, setCount] = React.useState(0);
  const divRef = React.useRef("initval");

  console.log("During Render", divRef.current);

  React.useEffect(() => {
    console.log("Inside useEffect CB", divRef.current); 
  }, []);

  return <button ref={divRef} onClick={() => setCount(count + 1)}>{count}</button>;
}

ReactDOM.render(<Counter />, document.getElementById("root"));
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="root"></div>

Understanding the flow:

  • When useRef is called React returns an object whose current property is set to the initial value provided ("initval" in our case).

  • So, the console log during first render prints the initial value.

  • Then the JSX is returned from the component, which updates the ref and sets it's current property to the DOM node.

  • Then the callback passed useEffect runs and since the ref has already been updated it prints the DOM node.

  • Then in next render (which can be triggered by clicking on the button), again we call useRef and React returns the same object and therefore this time the log during the render prints the DOM node.

about 4 years ago · Juan Pablo Isaza Relatório

0

use optional chaining to your textInput value

like this : textInput?.current?.blur();

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda