mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-24 19:28:15 +03:00
24 lines
650 B
JavaScript
24 lines
650 B
JavaScript
|
import { useEffect, useRef } from 'preact/hooks';
|
||
|
import { useLocation } from 'react-router-dom';
|
||
|
|
||
|
// Hook that runs a callback when the location changes
|
||
|
// Won't run on the first render
|
||
|
|
||
|
export default function useLocationChange(fn) {
|
||
|
if (!fn) return;
|
||
|
const location = useLocation();
|
||
|
const currentLocationRef = useRef(location.pathname);
|
||
|
useEffect(() => {
|
||
|
// console.log('location', {
|
||
|
// current: currentLocationRef.current,
|
||
|
// next: location.pathname,
|
||
|
// });
|
||
|
if (
|
||
|
currentLocationRef.current &&
|
||
|
location.pathname !== currentLocationRef.current
|
||
|
) {
|
||
|
fn?.();
|
||
|
}
|
||
|
}, [location.pathname, fn]);
|
||
|
}
|