Skip to content

Commit 89e3cd6

Browse files
fix(react): useIonRouter hook has stable router reference (#25000)
resolves #24987
1 parent d78cda2 commit 89e3cd6

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

packages/react/src/components/IonRouterContext.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AnimationBuilder } from '@ionic/core/components';
2-
import React, { useContext } from 'react';
2+
import React, { useContext, useMemo } from 'react';
33

44
import { RouteAction, RouterDirection, RouterOptions } from '../models';
55
import { RouteInfo } from '../models/RouteInfo';
@@ -39,13 +39,17 @@ export const IonRouterContext = React.createContext<IonRouterContextState>({
3939
*/
4040
export function useIonRouter(): UseIonRouterResult {
4141
const context = useContext(IonRouterContext);
42-
return {
43-
back: context.back,
44-
push: context.push,
45-
goBack: context.back,
46-
canGoBack: context.canGoBack,
47-
routeInfo: context.routeInfo,
48-
};
42+
43+
return useMemo(
44+
() => ({
45+
back: context.back,
46+
push: context.push,
47+
goBack: context.back,
48+
canGoBack: context.canGoBack,
49+
routeInfo: context.routeInfo,
50+
}),
51+
[context.back, context.push, context.canGoBack, context.routeInfo]
52+
);
4953
}
5054

5155
export type UseIonRouterResult = {

0 commit comments

Comments
 (0)