Skip to content

Instantly share code, notes, and snippets.

@myogeshchavan97
Last active April 4, 2021 17:30
Show Gist options
  • Save myogeshchavan97/dc32f7d80d59b10d0b90370ccc825248 to your computer and use it in GitHub Desktop.
Save myogeshchavan97/dc32f7d80d59b10d0b90370ccc825248 to your computer and use it in GitHub Desktop.
Custom Hook
import React, { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom";
const useTimer = () => {
const [date, setDate] = useState(new Date());
let interval = useRef();
useEffect(() => {
interval.current = setInterval(() => {
setDate(new Date());
}, 1000);
return () => {
clearInterval(interval.current);
};
}, []);
return date.toLocaleTimeString();
};
const App = () => {
const time = useTimer();
return <div>{time}</div>;
};
ReactDOM.render(<App />, document.getElementById("root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment