# useIdle

Source code is hosted on [GitHub](https://github.com/corets/use-idle)

{% tabs %}
{% tab title="yarn" %}

```bash
yarn add @corets/use-idle
```

{% endtab %}

{% tab title="npm" %}

```
npm install --save @corets/use-idle
```

{% endtab %}
{% endtabs %}

## useIdle() <a href="#useidle" id="useidle"></a>

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

```typescript
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>
  )
}
```
