Skip to main content Link Search Menu Expand Document (external link)

useIdleCallback

Use this hook if you want to delay the execution of a function to a time when the browser is idle.

A good use-case for this might be user tracking, for instance.

See requestIdleCallback() to learn more about the concept of “idle callbacks”.

Example

import { useIdleCallback } from 'react-timing-hooks'

// Track button click when idle
const trackClickWhenIdle = useIdleCallback(trackClick)

return <button onClick={trackClickWhenIdle}>Track me!</button>

API

Params

useIdleCallback(callback, options)

Name Default value Description
callback This is required. Callback that will be invoked when the browser is idle.
options undefined options for requestIdleCallback.

Return value

A function that executes your callback as an idle callback.

Notes

Any registered idle callbacks will be canceled on unmount.

This hook will print a console warning if the browser doesn’t support requestIdleCallback.