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

380
Visualizações
How to fix error Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop using react?

i am getting uncaught error. too many re-renders.

what i am trying to do?

when a button is clicked start function is called and modal popup opens. once the start function is finished i have to close the modal popup.

below is the code,

enum CheckStatus {
    INITIAL,
    STARTING,
    STARTED,
    FINISHED,
    FAILED,
}

const useSomeHook = (id) => {
    const [status, setStatus] = React.useState(CheckStatus.INITIAL);
    
    const start = React.useCallback(async () => {
        setStatus(CheckStatus.STARTING);

        let data= undefined;
        try {
            const { data: checkData } = await startCheck({
                variables: { id },
            });
            data = checkData;
        }
        //some logic
         React.useEffect(() => {
             if (status === CheckStatus.STARTED ) {
                 stopPolling && stopPolling();
                 setStatus(Check Status.FINISHED);

                 //once status is finished then i have to close modal popup
             }
         }

         return {
             start,
             status,
         };
     };


const Main = () => {
    const [openModal,setOpenModal] = React.useState(false);
    const {start,status} = useSomeHook(id);

    return (
        {openModal && (<Modal />)}
    );
}

Now i want to set the openModal state to false when status is finished. so i do like below,

const Main = () => {
    const [openModal,setOpenModal] = React.useState(false);
    const {start,status} = useSomeHook(id);
    
    if (status === CheckStatus.FINISHED) {
        setOpenModal(false); 
    } //this gives error
    return (
        {openModal && (<Modal />)}
    );
}

but gives this error

Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop

how can i fix this? is this right solution?

also is there a way to set the setOpenModal inside the useSomeHook as soon as status is set to FINISHED?

could someone help me fix this. thanks.

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

0

I'm not 100% sure, but I believe your issues is that you are setting a state every render, which in turn will always re-render, and re-set the state. The infinite loop. To work around this you should probably use useEffect

const Main = () => {
    const [openModal,setOpenModal] = React.useState(false);
    const {start,status} = useSomeHook(id);

    React.useEffect(() => {
      if (status === CheckStatus.FINISHED) {
        setOpenModal(false); 
      }
    }, [status]);
    
    
    return (
        {openModal && (<Modal />)}
    );
}

which would only preform the if check whenever status is changed, instead of on every render.

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