Countdown Timer

A simple countdown timer example.

jsxCopy codeimport React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';

function CountdownTimer() {
  const [time, setTime] = useState(60);

  useEffect(() => {
    if (time > 0) {
      const timer = setInterval(() => setTime(time - 1), 1000);
      return () => clearInterval(timer);
    }
  }, [time]);

  return (
    <div>
      <h1>Countdown Timer</h1>
      <h2>{time} seconds remaining</h2>
    </div>
  );
}

ReactDOM.render(<CountdownTimer />, document.getElementById('root'));

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *