From 3a401dc316b6b0c29555e06501b98a275b7b9433 Mon Sep 17 00:00:00 2001 From: mbalsdon Date: Tue, 29 Mar 2022 21:00:36 -0700 Subject: [PATCH] add polling hook used for upcoming meetings notifications --- src/redux/hooks.tsx | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/redux/hooks.tsx b/src/redux/hooks.tsx index 36b5ea2..797f6a6 100644 --- a/src/redux/hooks.tsx +++ b/src/redux/hooks.tsx @@ -1,6 +1,30 @@ +import { useLayoutEffect, useEffect, useRef } from "react"; import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"; import type { RootState, AppDispatch } from "./store"; // Use throughout your app instead of plain `useDispatch` and `useSelector` export const useAppDispatch = () => useDispatch(); export const useAppSelector: TypedUseSelectorHook = useSelector; + +const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect; +export function useInterval(callback: () => void, delay: number | null) { + const savedCallback = useRef(callback); + + // Remember the latest callback if it changes. + useIsomorphicLayoutEffect(() => { + savedCallback.current = callback; + }, [callback]); + + // Set up the interval. + useEffect(() => { + // Don't schedule if no delay is specified. + // Note: 0 is a valid value for delay. + if (!delay && delay !== 0) { + return; + } + + const id = setInterval(() => savedCallback.current(), delay); + + return () => clearInterval(id); + }, [delay]); +} \ No newline at end of file