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

402
Visualizações
Server Error Error: Too many re-renders. React limits the number of renders to prevent an infinite loop

I am trying to create a simple stopwatch app and getting the following error. I have tried the solutions for similar question yet have been unable to identify the real cause and solve it

here's my code

import React, { useState, useEffect } from "react"
function Watch() {
  const [iter, setiter] = useState(0);
  const [running, setRunning] = useState(false);
  const [time, setTime] = useState("0:0:0");
  const [start, setStart] = useState([]);
  const [today, setToday] = useState([]);
  useEffect(() => {
    let interval;
    if (iter == 0) {
      var today = new Date()
      setStart([today.getHours(), today.getMinutes(), today.getSeconds()])
    }
    if (running) {
      interval = setInterval(() => {
        setiter((previter) => previter + 10);
      }, 10);
    } else if (!running) {
      clearInterval(interval);
    }
    var t = new Date()
    setToday([t.getHours(), t.getMinutes(), t.getSeconds()])
    return () => clearInterval(interval);
  }, [running]);
  return (
    <div className="stopwatch">
      <div className="numbers">
        {setTime(running ? (String(today[0] - start[0]) + ':' + String(today[1] - start[1]) + ':' + String(today[2] - start[2])) : time)}
        <span>{time}</span>
      </div>
      <div className="buttons">
        <button onClick={() => setRunning(true)}>Start</button>
        <button onClick={() => setRunning(false)}>Stop</button>
        <button onClick={() => setTime("0:0:0")}>Reset</button>
      </div>
    </div>
  );
};
export default Watch

Please note I am beginner at Next.js this is one of my first project.

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

0

Please consider this part again. The error comes from the below line:

<div className="numbers">
    {setTime(running ? (String(today[0] - start[0]) + ':' + String(today[1] - start[1]) + ':' + String(today[2] - start[2])) : time)}
    <span>{time}</span>
</div>

Please set time while component mounting using useEffect hook rather than now.

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