Hooks
use-cd
2/4/2026, 11:37:40 AM modified by MarvinA hook to handle CD, including timestamp management, interval management, and callback management.
Installation
This hook is used to handle CD, including timestamp management, interval management, and callback management.
Preview
timestamp: 1770205114512
'use client'import { useRef, useSyncExternalStore } from 'react';const useCD = (interval?: number, onCD?: (cd: number) => void) => { const timestamp = useRef<number>(Date.now()); return useSyncExternalStore( (callback) => { const intervalId = setInterval(() => { timestamp.current = Date.now(); onCD?.(timestamp.current); callback(); }, interval || 1000); return () => clearInterval(intervalId); }, () => timestamp.current, () => timestamp.current, );};export default useCD;'use client'import useCD from "."export default function (){ const value = useCD() return <div>timestamp: {value}</div>}Git Commit History(1 commits)
feat: us cd
Marvin
2月4日 11:37
ba8670a5