@@ -4,6 +4,15 @@ import useCommittedRef from './useCommittedRef'
44/**
55 * Creates a `setInterval` that is properly cleaned up when a component unmounted
66 *
7+ * ```tsx
8+ * function Timer() {
9+ * const [timer, setTimer] = useState(0)
10+ * useInterval(() => setTimer(i => i + 1), 1000)
11+ *
12+ * return <span>{timer} seconds past</span>
13+ * }
14+ * ```
15+ *
716 * @param fn an function run on each interval
817 * @param ms The milliseconds duration of the interval
918 */
@@ -12,22 +21,53 @@ function useInterval(fn: () => void, ms: number): void
1221/**
1322 * Creates a pausable `setInterval` that is properly cleaned up when a component unmounted
1423 *
24+ * ```tsx
25+ * const [paused, setPaused] = useState(false)
26+ * const [timer, setTimer] = useState(0)
27+ *
28+ * useInterval(() => setTimer(i => i + 1), 1000, paused)
29+ *
30+ * return (
31+ * <span>
32+ * {timer} seconds past
33+ *
34+ * <button onClick={() => setPaused(p => !p)}>{paused ? 'Play' : 'Pause' }</button>
35+ * </span>
36+ * )
37+ * ```
38+ *
1539 * @param fn an function run on each interval
1640 * @param ms The milliseconds duration of the interval
1741 * @param paused Whether or not the interval is currently running
1842 */
1943function useInterval ( fn : ( ) => void , ms : number , paused : boolean ) : void
2044
2145/**
22- * Creates a pausable `setInterval` that is properly cleaned up when a component unmounted
46+ * Creates a pausable `setInterval` that _fires_ immediately and is
47+ * properly cleaned up when a component unmounted
48+ *
49+ * ```tsx
50+ * const [timer, setTimer] = useState(-1)
51+ * useInterval(() => setTimer(i => i + 1), 1000, false, true)
52+ *
53+ * // will update to 0 on the first effect
54+ * return <span>{timer} seconds past</span>
55+ * ```
2356 *
2457 * @param fn an function run on each interval
2558 * @param ms The milliseconds duration of the interval
2659 * @param paused Whether or not the interval is currently running
2760 * @param runImmediately Whether to run the function immediately on mount or unpause
2861 * rather than waiting for the first interval to elapse
62+ *
63+
2964 */
30- function useInterval ( fn : ( ) => void , ms : number , paused : boolean , runImmediately : boolean ) : void
65+ function useInterval (
66+ fn : ( ) => void ,
67+ ms : number ,
68+ paused : boolean ,
69+ runImmediately : boolean ,
70+ ) : void
3171
3272function useInterval (
3373 fn : ( ) => void ,
0 commit comments