Vercel/src/utility/useOnPathChange.ts
2025-01-18 22:41:49 -06:00

15 lines
345 B
TypeScript

import { usePathname } from 'next/navigation';
import { useEffect, useRef } from 'react';
export default function useOnPathChange(onPathChange: () => void) {
const path = usePathname();
const initialPath = useRef(path);
useEffect(() => {
if (initialPath.current !== path) {
onPathChange();
}
}, [path, onPathChange]);
}