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

192
Views
React error in console - Maximum update depth exceeded when input length is more than 5

I have an input component that accepts input upto 5 characters. When the input length is more that 5, it shows a message Limit Exceeds (only upto 5 characters accepted).

Sample component code :

import { useEffect, useState } from "react";

export function ConditionalInput() {

  const [inputInfo, setInputInfo] = useState({ value: "", lengthCount: 0 });
  const [warningMessageVisible, setWarningMessageVisible] = useState(false);

  useEffect(() => {
    if (inputInfo.value.length > 5) {
      setInputInfo((s) => ({ ...s, lengthCount: 0 }));
    }
  }, [inputInfo, warningMessageVisible]);

  const onChange = ({ target }) => {
    setInputInfo((s) => ({ ...s, value: target.value }));
    if (target.value.length > 5) {
      setWarningMessageVisible(true);
    }
  };
  
  return (
    <div>
      <input type="text" value={inputInfo.value} onChange={onChange} />
      <div>Number of secrets: {inputInfo.lengthCount}</div>
      {warningMessageVisible && (
        <h3>Limit Exceeds (only upto 5 characters accepted)</h3>
      )}
    </div>
  );
}

On the UI everything looks ok but in the console I can see an error message printing in an infinite loop stating - Maximum update depth exceeded

Erro message screenshot

Note - Above code is not the actual code I'm using but a mock of it. It have the same error and working

How can I fix this error showing in console? any advise or answer is appreciated

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It's because of the dependencies array you've added setInputInfo Your useEffect will trigger whenever there is a change in the inputInfo. And inside the useEffect you're modifying the inputInfo. So basically it's creating an infinite loop which will keep rendering the view.

To fix that you can remove inputInfo from the dependency array of your useEffect.

about 4 years ago · Juan Pablo Isaza Report

0

This issue happen from this useEffect

useEffect(() => {
  if (inputInfo.value.length > 5) {
    setInputInfo((s) => ({ ...s, lengthCount: 0 }));
  }
}, [inputInfo, warningMessageVisible]);

This useEffect alway run when inputInfo change. So you need to remove the inputInfo at this useEffect like this:

useEffect(() => {
  if (inputInfo.value.length > 5) {
    setInputInfo((s) => ({ ...s, lengthCount: 0 }));
  }
}, [warningMessageVisible]);
about 4 years ago · Juan Pablo Isaza Report

0

You need to do the following changes in your code :

  • remove inputInfo from useEffect dependencies
  • instead of using infoInput.lengthCount use infoInput.value.length to show the length count.

Below is the working code

import { useEffect, useState } from "react";

export function ConditionalInput() {

  const [inputInfo, setInputInfo] = useState({ value: "", lengthCount: 0 });
  const [warningMessageVisible, setWarningMessageVisible] = useState(false);

  useEffect(() => {
    if (inputInfo.value.length > 5) {
      setInputInfo((s) => ({ ...s, lengthCount: 0 }));
    }
  }, [warningMessageVisible]);

  const onChange = ({ target }) => {
    setInputInfo((s) => ({ ...s, value: target.value }));
    if (target.value.length > 5) {
      setWarningMessageVisible(true);
    }
  };

  return (
    <div>
      <input type="text" value={inputInfo.value} onChange={onChange} />
      <div>Number of secrets: {inputInfo.value.length}</div>
      {warningMessageVisible && (
        <h3>Limit Exceeds (only upto 5 characters accepted)</h3>
      )}
    </div>
  );
}

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!