Download wp-calypso/client/lib/interval/use-interval.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/lib/interval/use-interval.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/lib/interval/use-interval.ts
-
curl -L -o use-interval.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/lib/interval/use-interval.ts
1.13 kB
| import { useEffect, useRef } from 'react'; | |
| import type { TimeoutMS } from 'calypso/types'; | |
| /** | |
| * useInterval implementation from @see https://overreacted.io/making-setinterval-declarative-with-react-hooks/ | |
| * | |
| * Used with explicit permission: | |
| * > Feel free to copy paste it in your project or put it on npm. | |
| * https://github.com/gaearon/overreacted.io/blob/80c0a314c5d855891220852788f662c2e8ecc7d4/src/pages/making-setinterval-declarative-with-react-hooks/index.md | |
| */ | |
| /** | |
| * Invoke a function on an interval. | |
| * @param callback Function to invoke | |
| * @param delay Interval timout in MS. `null` or `false` to stop the interval. | |
| */ | |
| export function useInterval( callback: () => void, delay: TimeoutMS | null | false ) { | |
| const savedCallback = useRef( callback ); | |
| // Remember the latest callback. | |
| useEffect( () => { | |
| savedCallback.current = callback; | |
| }, [ callback ] ); | |
| // Set up the interval. | |
| useEffect( () => { | |
| if ( delay === null || delay === false ) { | |
| return; | |
| } | |
| const tick = () => void savedCallback.current(); | |
| const id = setInterval( tick, delay ); | |
| return () => clearInterval( id ); | |
| }, [ delay ] ); | |
| } | |