Created
January 9, 2019 08:52
-
-
Save mohanramphp/af4f0267f5b1c3c0e726e18019eb2a0b to your computer and use it in GitHub Desktop.
DateTime Component using React hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
export const DateTime = () => { | |
const [dateTime, setDateTime] = useState(new Date()); | |
useEffect(() => { | |
const id = setInterval(() => setDateTime(new Date()), 1000); | |
return () => { | |
clearInterval(id); | |
} | |
}, []); | |
return ( | |
<h4>{`${dateTime.toLocaleDateString()} ${dateTime.toLocaleTimeString()}`}</h4> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OOOOO zo'r ekan lekin