mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-05-05 06:05:12 -04:00
* Add typescript parser and fix Flow errors in JS files * Uninstall packages from react-native/eslint-config * Fix all flow errors (or ignore them for unknowns
31 lines
728 B
JavaScript
31 lines
728 B
JavaScript
// @flow
|
|
|
|
import { useEffect, useRef } from "react";
|
|
|
|
function useInterval( callback:Function, delay: number | null ) {
|
|
const savedCallback = useRef( null );
|
|
|
|
// Remember the latest callback function
|
|
useEffect( () => {
|
|
if ( delay === null ) return;
|
|
savedCallback.current = callback;
|
|
}, [callback, delay] );
|
|
|
|
// Set up the interval
|
|
useEffect( () => {
|
|
function tick() {
|
|
if ( savedCallback && savedCallback.current !== null ) {
|
|
savedCallback.current();
|
|
}
|
|
}
|
|
if ( delay === null ) {
|
|
return;
|
|
}
|
|
const id = setInterval( tick, delay );
|
|
// eslint-disable-next-line consistent-return
|
|
return () => clearInterval( id );
|
|
}, [delay] );
|
|
}
|
|
|
|
export default useInterval;
|