useInterval

Schedule periodic actions like data polling or animations with useInterval.

Install:

Note: This hook depends on React’s experimental useEffectEvent.

npm i @uidotdev/usehooks@experimental react@experimental react-dom@experimental

Description:

The useInterval hook provides a convenient way to create and manage intervals. The hook sets up an interval that repeatedly invokes the callback function at the specified interval. The interval is automatically cleared when the component unmounts or when the interval duration changes. This hook is useful for scenarios where you need to perform a certain action or update the component periodically, such as polling for data updates or implementing animations.

Parameters

NameTypeDescription
cbfunctionThe callback function to be executed at the specified interval.
msnumberThe interval duration in milliseconds.

Return Value

NameTypeDescription
clearIntervalfunctionA function to clear the interval and stop the execution.

Demo:

Example:

More Hooks:

Sort: