react-use-date
Have you ever used new Date()
in React code, but noticed it didn't update?
The current time is: 2023-05-24T20:34:52.903Z
Instead, try using useDate()
:
import useDate from "react-use-date";
const Clock = () => {
const date = useDate();
return <div>The current time is: {date.toISOString()}</div>;
};
The current time is: 2023-05-24T20:34:52.904Z
Options by Example
option | result |
---|---|
useDate() | 20:34:52.904 |
useDate({ interval: 'second' }) | 20:34:52.904 |
useDate({ interval: 'minute' }) | 20:34:52.904 |
useDate({ interval: 'hour' }) | 20:34:52.904 |
useDate({ interval: 1000 }) | 20:34:52.904 |
Get It!
npm install react-use-date
- GitHub