feat(IonReactHashRouter): migrate to a functional component and react router 6

Co-authored-by: Sean Perkins <13732623+sean-perkins@users.noreply.github.com>
This commit is contained in:
Maria Hutt
2025-06-12 16:14:24 -07:00
parent 205a7056d7
commit 7ff8994d12

View File

@ -1,53 +1,46 @@
import type { Action as HistoryAction, History, Location as HistoryLocation } from 'history';
import { createHashHistory as createHistory } from 'history';
import React from 'react';
import type { BrowserRouterProps } from 'react-router-dom';
import { Router } from 'react-router-dom';
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';
interface IonReactHashRouterProps extends BrowserRouterProps {
history?: History;
}
export const IonReactHashRouter = ({ children }: PropsWithChildren<HashRouterProps>) => {
const location = useLocation();
const navigationType = useNavigationType();
export class IonReactHashRouter extends React.Component<IonReactHashRouterProps> {
history: History;
historyListenHandler?: (location: HistoryLocation, action: HistoryAction) => void;
const historyListenHandler = useRef<(location: HistoryLocation, action: HistoryAction) => void>();
constructor(props: IonReactHashRouterProps) {
super(props);
const { history, ...rest } = props;
this.history = history || createHistory(rest);
this.history.listen(this.handleHistoryChange.bind(this));
this.registerHistoryListener = this.registerHistoryListener.bind(this);
}
const registerHistoryListener = (cb: (location: HistoryLocation, action: HistoryAction) => void) => {
historyListenHandler.current = cb;
};
/**
* history@4.x passes separate location and action
* params. history@5.x passes location and action
* together as a single object.
* TODO: If support for React Router <=5 is dropped
* this logic is no longer needed. We can just assume
* a single object with both location and action.
* 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').
*/
handleHistoryChange(location: HistoryLocation, action: HistoryAction) {
const locationValue = (location as any).location || location;
const actionValue = (location as any).action || action;
if (this.historyListenHandler) {
this.historyListenHandler(locationValue, actionValue);
const handleHistoryChange = (location: HistoryLocation, action: HistoryAction) => {
if (historyListenHandler.current) {
historyListenHandler.current(location, action);
}
}
};
registerHistoryListener(cb: (location: HistoryLocation, action: HistoryAction) => void) {
this.historyListenHandler = cb;
}
useEffect(() => {
handleHistoryChange(location, navigationType);
}, [location, navigationType]);
render() {
const { children, ...props } = this.props;
return (
<Router history={this.history} {...props}>
<IonRouter registerHistoryListener={this.registerHistoryListener}>{children}</IonRouter>
</Router>
);
}
}
return (
<HashRouter>
<IonRouter registerHistoryListener={registerHistoryListener}>{children}</IonRouter>
</HashRouter>
);
};