hook-use-document-visibility

1.0.0 • Public • Published

useDocumentVisibility

This hook checks if the browser tab the user is in is active and how many times the page has been visited by the user


Example

import React from 'react'
import useDocumentVisibility from 'hook-use-document-visibility'

const LeaveTabCounter = () => {
  const { count, visible, onVisibilityChange } = useDocumentVisibility();

  useEffect(() => {
    onVisibilityChange((isVisible) => {
      //...
    });
    onVisibilityChange((isVisible) => {
      //...
    });
  }, [])

  return (
    <div>
      <span>
        You have left the page: {count} times
        Is the tab active? {visible ? 'yes' : 'no'}
      </span>
    </div>
  );
};

Package Sidebar

Install

npm i hook-use-document-visibility

Weekly Downloads

1

Version

1.0.0

License

ISC

Unpacked Size

6 kB

Total Files

4

Last publish

Collaborators

  • jamila_sh