mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-03 03:12:35 +02:00
38 lines
1 KiB
TypeScript
38 lines
1 KiB
TypeScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import {useEffect} from 'react';
|
|
import {useLocation} from '@docusaurus/router';
|
|
import type {Location} from 'history';
|
|
import {useDynamicCallback, usePrevious} from './reactUtils';
|
|
|
|
type LocationChangeEvent = {
|
|
location: Location;
|
|
previousLocation: Location | undefined;
|
|
};
|
|
|
|
type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
|
|
|
|
export function useLocationChange(onLocationChange: OnLocationChange): void {
|
|
const location = useLocation();
|
|
const previousLocation = usePrevious(location);
|
|
|
|
const onLocationChangeDynamic = useDynamicCallback(onLocationChange);
|
|
|
|
useEffect(() => {
|
|
if (!previousLocation) {
|
|
return;
|
|
}
|
|
|
|
if (location !== previousLocation) {
|
|
onLocationChangeDynamic({
|
|
location,
|
|
previousLocation,
|
|
});
|
|
}
|
|
}, [onLocationChangeDynamic, location, previousLocation]);
|
|
}
|