usePageTrackerStore()
usePageTrackerStore() can retrieve the required parameters and also allows you to select the needed parameters through state to avoid multiple re-renders of the component in React. following is the return type:
type PageTrackerState = { /** current page index */ pageIndex: number; /** correct `document.referrer` */ referrer: string; /** whether the current page is the first page */ isFirstPage: boolean; /** whether the current page is the last page */ isLastPage: boolean; /** whether the user navigated to the page via browser back/forward buttons or by clicking a link */ pageEvent?: PageEvent; // 'back' | 'forward' | 'push' | undefined. undefined for first visit. /** history browsing record */ pageHistory: string[]; // ['/', '/products', '/pdocuts/1', '/products/2', '/about', ...] /** * total page history length. * When user press `back` button, the `pageHistory`'s end link will become the current link. * You may need this total length to handle `history.go(N)` to forward N page. */ pageHistoryLength: number; }
Example
import { usePageTrackerStore } from 'react-page-tracker'; export const PageTrackerValue = () => { // get all state const state = usePageTrackerStore((state) => state); // get the specific state you'd like to use const pickState = usePageTrackerStore((state) => ({ isFirstPage: state.isFirstPage, referrer: state.referrer, pageHistory: state.pageHistory, })); return ( <div className="flex"> <div className="flex flex-col gap-3"> <pre className="rounded-md bg-gray-100 px-2 py-0.5 font-bold"> state: {JSON.stringify(state)} </pre> <pre className="rounded-md bg-gray-100 px-2 py-0.5 font-bold"> pickState: {JSON.stringify(pickState)} </pre> </div> </div> ); };