Skip to content

Instantly share code, notes, and snippets.

@aandrewww
Last active May 23, 2020 17:23
Show Gist options
  • Save aandrewww/045cecb6dc4e0b37bfb7a55d89be09ad to your computer and use it in GitHub Desktop.
Save aandrewww/045cecb6dc4e0b37bfb7a55d89be09ad to your computer and use it in GitHub Desktop.
import { useEffect, useState } from 'react';
import createActivityDetector from 'activity-detector';
// inspired by https://egghead.io/lessons/react-detect-user-activity-with-a-custom-useidle-react-hook
export const useInactivity = (options) => {
const [isIdle, setIsIdle] = useState(false);
useEffect(() => {
const activityDetector = createActivityDetector(options);
activityDetector.on('idle', () => setIsIdle(true));
activityDetector.on('active', () => setIsIdle(false));
return () => activityDetector.stop();
}, []);
return isIdle;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment