@milya505/react-usedocumentvisibility
v1.0.9
Published
React hook that:
Downloads
4
Readme
react-useDocumentVisibility
React hook that:
- Determines if the browser tab is currently active.
- Tracks how many times the tab has become inactive since the component's initialization.
- Provides a function to subscribe to changes in the activity of the current tab.
Installation
Via package manager:
npm i @milya505/react-usedocumentvisibility
Usage
import React from "react";
import { useDocumentVisibility } from "@milya505/react-usedocumentvisibility";
const LeaveTabCounter = () => {
const { count, visible, onVisibilityChange } = useDocumentVisibility();
useEffect(() => {
onVisibilityChange((isVisible) => {
console.log("first handler", isVisible);
});
const unsubscribeSecondHandler = onVisibilityChange((isVisible) => {
console.log("second handler", isVisible);
});
setTimeout(() => unsubscribeSecondHandler(), 5000);
}, []);
return (
<div>
<span>
Вы покинули страницу: {count} раз Вкладка активна?{" "}
{visible ? "да" : "нет"}
</span>
</div>
);
};