Features
Fetch on a regular interval

Fetch on a regular interval

You can use useInterval to fetch a resource on a regular interval.

useInterval(query, interval) 

Motivation

You know that some data is regularly updated.

Example

function useFetchAndIntervalMixture(query: Query, interval: number) {
  useFetch(query)
  useInterval(query, interval)
}
 
function useHelloMix() {
  const query = useSchema(getHelloSchema, [])
  useFetchAndIntervalMixture(query, 1000)
  return query
}

Implementation

export function useInterval(query: Query, interval: number) {
  const { fetch } = query
 
  useEffect(() => {
    if (!interval) return
    const i = setInterval(fetch, interval)
    return () => clearInterval(i)
  }, [fetch, interval])
}

See also