a interval which is counting every second a counter higher in react in javascript

import React, { useState, useEffect } from 'react';

const CounterComponent = () => {
  const [counter, setCounter] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      setCounter(prevCounter => prevCounter + 1);
    }, 1000); // 1000 milliseconds = 1 second

    return () => clearInterval(interval);
  }, []);

  return <div>{counter}</div>;
};

export default CounterComponent;
411 chars
18 lines

related categories

gistlibby LogSnag