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

166
Visualizações
Getting error in countdown timer using react-countdown

I am currently building a countdown timer where a user inputs the time in minutes and can start and stop the clock. I am facing two errors,

  1. I have made a state running when the clock is running and based on this state I am returning the buttons (either stop/ start), but I am getting the following error while using this function.

    Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render.

  2. The autostart prop of the react-Countdown component is not working in this case. (link for doc.

I have attached both codesandbox link and code also.

Here is the codesanbox link

My current code is

import React, { useState } from "react";
import Countdown from "react-countdown";

const Stopwatch = () => {
  const [running, setRunning] = useState(true);
  const [time, setTime] = useState(6);

  const handleOnChange = (e) => {
    console.log(e.target.value);
    setTime(e.target.value);
  };

  function buttons(running) {
   if(running ){
     return (
      <div>
        <button style={{ marginTop: "15px" }} onClick={() => setRunning(false)}>
          Stop
        </button>
      </div>
    )
  }
  else {
    return (
      <div>
        <button onClick={() => setRunning(true)}>Start</button>
      </div>
    );
  }
}
  
  console.log("time = " + time);
  return (
    <>
      <div>
        <label htmlFor="time"> Enter time in minutes </label>
        <input
          type="text"
          id="time"
          name="time"
          value={time}
          onChange={(e) => handleOnChange(e)}
        />
      </div>
      <div>
        <h1>
          <Countdown
            date={Date.now() + { time } * 4000}
            precision={3}
            autoStart={running}
            renderer={({ hours, minutes, seconds, completed }) => {
              return (
                <span>
                  {hours}:{minutes}:{seconds}
                </span>
              );
            }}
          />
        </h1>
      </div>
      {buttons}
    </>
  );
};

export default Stopwatch;

Please tell the error.

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

0

I had solved the first error by using the function inside DOM only like this:

import React, { useState } from "react";
import Countdown from "react-countdown";

const Stopwatch = () => {
  const [running, setRunning] = useState(false);
  const [time, setTime] = useState(6);

  const handleOnChange = (e) => {
    console.log(e.target.value);
    setTime(e.target.value);
  };


  console.log("time = " + time);
  return (
    <>
      <div>
        <label htmlFor="time"> Enter time in minutes </label>
        <input
          type="text"
          id="time"
          name="time"
          value={time}
          onChange={(e) => handleOnChange(e)}
        />
      </div>
      <div>
        <h1>
          <Countdown
            date={Date.now() + { time } * 4000}
            precision={3}
            autoStart={running}
            renderer={({ hours, minutes, seconds, completed }) => {
              return (
                <span>
                  {hours}:{minutes}:{seconds}
                </span>
              );
            }}
          />
        </h1>
      </div>
      {running ? (
        <div>
          <button
            style={{ marginTop: "15px" }}
            onClick={() => setRunning(false)}
          >
            Stop
          </button>
        </div>
      ) : (
        <div>
          <button onClick={() => setRunning(true)}>Start</button>
        </div>
      )}
    </>
  );
};

export default Stopwatch;

But still, the autostart is not working, so after inputting the value in the text field and pressing start, the clock does not appear.

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