Skip to main content

useIdle

A hook to detect idle state of user. Idle state means user have not interacted with the page for a while.

import { useIdle } from 'react-use-custom-hooks';

Usage example

const idle = useIdle(5000);

Playground

Pass the timeout value in milliseconds to useIdle hook as the first parameter. Default timeout is 2000ms.

Live Editor
Result
Loading...

Events

By default this hook will listen to keydown, mousemove, mousedown, touchmove, touchstart, click and scroll events. You can pass events array in options parameter to listen to additional events.

Live Editor
Result
Loading...

Listen only to specific events

Pass onlyPropEvents value true in options object to listen only to events passed in events in options.

Live Editor
Result
Loading...

API

interface Options {
events?: Array<string>;
initialState?: boolean;
onlyPropEvents?: boolean;
}

export function useIdle(timeout: number, options: Options): void;

Options

PropertyDescriptionTypeDefault
eventsList of events to be listen to track idle statusArray<string>[]
initialStateInitial state of hookBooleantrue
onlyPropEventsPass true to listen only to the events passed in props for idle detectionBooleanfalse