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

193
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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