use-page-view
Version:
A React hook for tracking page views and user engagement time. This hook provides real-time tracking of how long users spend on a page, their activity status, and the ability to persist time tracking across page reloads.
47 lines (45 loc) • 1.13 kB
TypeScript
import React from "react";
//#region src/hooks/use-interval.d.ts
/**
* A hook that creates an interval that can be paused and resumed
*
* @param callback - The function to call on each interval tick
* @param delay - The interval delay in milliseconds. Set to null to pause the interval
*
* @example
* ```tsx
* function Timer() {
* const [count, setCount] = React.useState(0);
*
* useInterval(() => {
* setCount(c => c + 1);
* }, 1000);
*
* return <div>Count: {count}</div>;
* }
* ```
*
* @example
* ```tsx
* function PausableTimer() {
* const [count, setCount] = React.useState(0);
* const [isPaused, setIsPaused] = React.useState(false);
*
* useInterval(() => {
* setCount(c => c + 1);
* }, isPaused ? null : 1000);
*
* return (
* <div>
* <div>Count: {count}</div>
* <button onClick={() => setIsPaused(p => !p)}>
* {isPaused ? 'Resume' : 'Pause'}
* </button>
* </div>
* );
* }
* ```
*/
declare function useInterval(callback: () => void, delay: number | null): React.RefObject<number | null>;
//#endregion
export { useInterval };