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

378
Visualizações
How to call a function containing state change from target.value to useEffect() function?

I have an error Cannot read properties of undefined (reading 'target') for this code:

const [title, setTitle] = useState("");

const handleChangeTitle = (e) => {
  setTitle(e.target.value);
};

useEffect(() => {
  handleChangeTitle()
}, [title]);

return (
  <div className="App">
    <input
      name="title"
      type="text"
      value={title}
      onChange={handleChangeTitle}
    />
    <p>{title}</p>
  </div>
);

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Remove the useEffect altogether. useEffect is for lifecycle events such as on initial render. What you're doing is when the input changes you update your state. That's fine call handleChangeTitle exactly as you are doing. However, your useEffect has a dependency on the title variable. That means anytime it changes it will fire every time that variable's value changes.

input -> value change -> handleChangeTitle updates state "title" -> useEffect fires -> calls handleChangeTitle AGAIN -> cycle over and over until app crashes

Besides the already called out missing parameter to handleChangeTitle there is nothing wrong with your code. It's working as intended. It's just flawed in its design. I would suggest reading up on the correct usage of each hook here

about 4 years ago · Santiago Trujillo Relatório

0

you are already watching [title] inside you useEffect , whenever onChange event fired the function will called , as Szymon Said you don't your onChange inside your useEffect

about 4 years ago · Santiago Trujillo 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