useIdle

Detect when a user goes idle.

Source code is hosted on GitHub

yarn add @corets/use-idle

useIdle()

Detect when a user has been inactive for a certain amount of time:

import React, { useEffect } from "react"
import { useIdle } from "@corets/use-idle"

const Example = () => {
  const isIdle = useIdle({
    // check every 10s
    interval: 10 * 1000,
    // regard user as idle after 30min
    threshold: 30 * 60 * 1000,
    // specify events that should reset the idle status (default: ["mousemove", "mouseup", "keyup", "focus", "resize"])
    events: ["mousemove"],
    // local storage key for the last activity timestamp (default: last-activity)
    storageKey: "custom-storage-key"
  })
  
  useEffect(() => {
    if (isIdle) {
      // do something, for example: logout()
    }
  }, [isIdle])
  
  return (
    <div>My App</div>
  )
}

Last updated