mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-24 03:08:13 +03:00
965f948899
Seems more robust
23 lines
650 B
JavaScript
23 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]);
|
|
}
|