mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-11 21:29:09 -05:00
26 lines
699 B
TypeScript
26 lines
699 B
TypeScript
import * as React from "react";
|
|
|
|
/**
|
|
* Forces the component to rerender periodically. Returns the current `Date` at
|
|
* the time of the latest tick — callers should consume this value (e.g. pass
|
|
* it to date-fns) so React Compiler can see the state is observable and won't
|
|
* memoize the rerender away.
|
|
*/
|
|
export function useAutoRerender(every?: "second" | "ten seconds"): Date {
|
|
const [now, setNow] = React.useState(() => new Date());
|
|
|
|
React.useEffect(() => {
|
|
const intervalTime = !every || every === "second" ? 1000 : 10000;
|
|
|
|
const interval = setInterval(() => {
|
|
setNow(new Date());
|
|
}, intervalTime);
|
|
|
|
return () => {
|
|
clearInterval(interval);
|
|
};
|
|
}, [every]);
|
|
|
|
return now;
|
|
}
|