Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda