How to add Live Clock in React js

Hi there, today we will learn how to add a live clock to your react js web applications. We are going to implement this using react functional component. Here is the code for time and date:
import React, { useState, useEffect } from "react";
export const DateTime = () => {
var [date, setDate] = useState(new Date());
useEffect(() => {
var timer = setInterval(() => setDate(new Date()), 1000);
return function cleanup() {
clearInterval(timer);
}
})
return (
<div>
Time : {date.toLocaleTimeString()}
Date : {date.toLocaleDateString()}
</div>
)
}
export default DateTime;
view raw DateTime.js hosted with ❤ by GitHub

0 Comments