mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 18:17:31 +08:00
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import type { Action as HistoryAction, Location as HistoryLocation } from 'history';
|
|
import type { PropsWithChildren } from 'react';
|
|
import React, { useEffect, useRef } from 'react';
|
|
import type { HashRouterProps } from 'react-router-dom';
|
|
import { HashRouter, useLocation, useNavigationType } from 'react-router-dom';
|
|
|
|
import { IonRouter } from './IonRouter';
|
|
|
|
export const IonReactHashRouter = ({ children }: PropsWithChildren<HashRouterProps>) => {
|
|
const location = useLocation();
|
|
const navigationType = useNavigationType();
|
|
|
|
const historyListenHandler = useRef<(location: HistoryLocation, action: HistoryAction) => void>();
|
|
|
|
const registerHistoryListener = (cb: (location: HistoryLocation, action: HistoryAction) => void) => {
|
|
historyListenHandler.current = cb;
|
|
};
|
|
|
|
/**
|
|
* Processes navigation changes within the application.
|
|
*
|
|
* Its purpose is to relay the current `location` and the associated
|
|
* `action` ('PUSH', 'POP', or 'REPLACE') to any registered listeners,
|
|
* primarily for `IonRouter` to manage Ionic-specific UI updates and
|
|
* navigation stack behavior.
|
|
*
|
|
* @param location The current browser history location object.
|
|
* @param action The type of navigation action ('PUSH', 'POP', or
|
|
* 'REPLACE').
|
|
*/
|
|
const handleHistoryChange = (location: HistoryLocation, action: HistoryAction) => {
|
|
if (historyListenHandler.current) {
|
|
historyListenHandler.current(location, action);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
handleHistoryChange(location, navigationType);
|
|
}, [location, navigationType]);
|
|
|
|
return (
|
|
<HashRouter>
|
|
<IonRouter registerHistoryListener={registerHistoryListener}>{children}</IonRouter>
|
|
</HashRouter>
|
|
);
|
|
};
|