Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!