A React hook that makes your component aware of time.
npm install use-time-react-hook -S
Or
yarn add use-time-react-hook
Examples:
- Refresh data at a latest time range:
import { useTime } from "use-time-react-hook";
const App = () => {
const [time] = useTime({ range: "last 1 min", interval: "1 sec" });
useEffect(() => {
const {from, to} = time
// refresh data of last 1 minute on each second
refreshData(from, to);
}, [time]);
return <div>...</div>;
};
import { useTime } from "use-time-react-hook";
const Clock = () => {
const [time] = useTime();
return <div> Now is ${new Date(time).toString()} </div>;
};
range
(optional): A string followslast x y
, wherex
is number,y
is time unitinterval
(optional): A string followsx y
, wherex
is number,y
is time unit
time
(object|number): If range is defined,time
is a range with{from, to}
extracted from the defined range. Otherwise, it is millisecond value returned byDate#getTime()
initialTime
(number): Millisecond returned byDate#getTime()
at the begining of callinguseTime()
duration
(number): equaltime - initialTime
- Can be millisecond, sec, min, hour. All supported time units are defined here
MIT