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

200
Visualizações
How onClick is called always when the page is loaded? Error: Maximum update depth exceeded

I have this code to get the user input and send it back to the dispatch

import React, { useState } from 'react';

export default function Form(props) {
  const [book, setBook] = useState({
    title: '',
    author: '',
  });

  const onChange = (e) => {
    if (book[e.target.name] !== e.target.value) {
      setBook({
        ...book,
        [e.target.name]: e.target.value,
      });
    }
  };
  const { add } = props;
  return (
    <form>
      <input onChange={onChange} type="text" name="title" placeholder="Title" />
      <input onChange={onChange} type="text" name="author" placeholder="Author" />
      <button type="button" onClick={add(book.title, book.author)}>Add Book</button>
    </form>
  );
}

The add function is this:

  const submitBookToStore = (title, author) => {
    const newBook = {
      id: uuidv4(),
      title,
      author,
    };
    dispatch(addBook(newBook));
  };

Then I see this error:

Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops. 

The strange behaviour is it calls onClick when the page is loaded when I did not clicked!!

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

0

If you call it like this:

onClick={add(book.title, book.author)}

It will run on render. Try this instead:

onClick={() => add(book.title, book.author)}

That is because add() indicates a function call where as in your onChange, you are just giving a function to be called onChange={onChange} the difference lies in the parentheses ()

about 4 years ago · Juan Pablo Isaza Relatório

0

Try the following:

import React, { useState } from 'react';

export default function Form(props) {
  const { add } = props;
  const [book, setBook] = useState({
    title: '',
    author: '',
  });

  const onChange = (e) => {
    if (book[e.target.name] !== e.target.value) {
      setBook({
        ...book,
        [e.target.name]: e.target.value,
      });
    }
  };
  const handleAdd = () => { add(book.title, book.author) }
  return (
    <form>
      <input onChange={onChange} type="text" name="title" placeholder="Title" />
      <input onChange={onChange} type="text" name="author" placeholder="Author" />
      <button type="button" onClick={handleAdd}>Add Book</button>
    </form>
  );
}

Hopefully that helps

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