useIdleTimer

The useIdleTimer hook can be used with functional React components. It takes IIdleTimerProps as configuration options and returns IIdleTimer.

Import#

import { useIdleTimer } from 'react-idle-timer'

Usage#

A minimal example with the bare essential configuration.

import { useIdleTimer } from 'react-idle-timer'
export const App = () => {
const onIdle = () => {
// Close Modal Prompt
// Do some idle action like log out your user
}
const onActive = (event) => {
// Close Modal Prompt
// Do some active action
}
const idleTimer = useIdleTimer({ onIdle, onActive })
return (
<HomePage />
)
}

A full example with all available props and methods. Props are set to their default value.

import { useIdleTimer } from 'react-idle-timer'
export const App = () => {
const onPrompt = () => {
// Fire a Modal Prompt
}
const onIdle = () => {
// Close Modal Prompt
// Do some idle action like log out your user
}
const onActive = (event) => {
// Close Modal Prompt
// Do some active action
}
const onAction = (event) => {
// Do something when a user triggers a watched event
}
const {
start,
reset,
activate,
pause,
resume,
isIdle,
isPrompted,
isLeader,
getTabId,
getRemainingTime,
getElapsedTime,
getLastIdleTime,
getLastActiveTime,
getTotalIdleTime,
getTotalActiveTime
} = useIdleTimer({
onPrompt,
onIdle,
onActive,
onAction,
timeout: 1000 * 60 * 20,
promptTimeout: 0,
events: [
'mousemove',
'keydown',
'wheel',
'DOMMouseScroll',
'mousewheel',
'mousedown',
'touchstart',
'touchmove',
'MSPointerDown',
'MSPointerMove',
'visibilitychange'
],
immediateEvents: [],
debounce: 0,
throttle: 0,
eventsThrottle: 200,
element: document,
startOnMount: true,
startManually: false,
stopOnIdle: false,
crossTab: false,
name: 'idle-timer',
syncTimers: 0,
leaderElection: false
})
return (
<HomePage />
)
}

For more examples, see the Features section.

Made withby Randy Lebeau