fix(react): support history@5 in preparation for react router 6 (#23297)

resolves #23294
This commit is contained in:
Liam DeBeasi
2021-05-14 09:03:15 -04:00
committed by GitHub
parent eb10a2ab0c
commit 4da5216b4f
4 changed files with 54 additions and 8 deletions

View File

@ -25,11 +25,21 @@ export class IonReactRouter extends React.Component<IonReactRouterProps> {
this.registerHistoryListener = this.registerHistoryListener.bind(this);
}
/**
* 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.
*/
handleHistoryChange(location: HistoryLocation, action: HistoryAction) {
if (this.historyListenHandler) {
this.historyListenHandler(location, action);
}
}
const locationValue = (location as any).location || location;
const actionValue = (location as any).action || action;
if (this.historyListenHandler) {
this.historyListenHandler(locationValue, actionValue);
}
}
registerHistoryListener(cb: (location: HistoryLocation, action: HistoryAction) => void) {
this.historyListenHandler = cb;