You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/angular/navigation.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -201,7 +201,9 @@ To get started with standalone components [visit Angular's official docs](https:
201
201
202
202
## Live Example
203
203
204
-
If you would prefer to get hands on with the concepts and code described above, please checkout our [live example](https://stackblitz.com/edit/ionic-angular-routing?file=src/app/app-routing.module.ts) of the topics above on StackBlitz.
204
+
import NavigationPlayground from '@site/static/usage/v9/navigation/index.md';
Since the parent route already matches `/dashboard/*`, the child routes use **relative paths**. The `index` route matches the parent path (`/dashboard`) and `"users/:id"` resolves to `/dashboard/users/:id`. Absolute paths (e.g., `path="/dashboard/users/:id"`) still work if you prefer explicit full paths.
73
69
70
+
Note the `ionPage` prop on `IonRouterOutlet`. When a component serves as a nested outlet rendered directly by a `Route` in a parent outlet, the inner `IonRouterOutlet` must include the `ionPage` prop. Without it, router outlets can overlap during navigation and cause broken transitions. Wrapping the outlet in an `IonPage` is not needed and should be avoided in this case.
71
+
74
72
These routes are grouped in an `IonRouterOutlet`, let's discuss that next.
75
73
76
-
## IonRouterOutlet
74
+
## Components
75
+
76
+
### IonRouterOutlet
77
77
78
78
The `IonRouterOutlet` component provides a container for Routes that render Ionic "pages". When a page is in an `IonRouterOutlet`, the container controls the transition animation between the pages as well as controls when a page is created and destroyed, which helps maintain the state between the views when switching back and forth between them.
79
79
80
80
The `DashboardPage` above shows a users list page and a details page. When navigating between the two pages, the `IonRouterOutlet` provides the appropriate platform page transition and keeps the state of the previous page intact so that when a user navigates back to the list page, it appears in the same state as when it left.
81
81
82
82
An `IonRouterOutlet` should only contain `Route`s. Any other component should be rendered either as a result of a `Route` or outside of the `IonRouterOutlet`.
83
83
84
-
## Fallback Route
84
+
###Fallback Route
85
85
86
86
A common routing use case is to provide a "fallback" route to be rendered in the event the location navigated to does not match any of the routes defined.
87
87
@@ -90,38 +90,30 @@ We can define a fallback route by placing a `Route` component with a `path` of `
Here, we see that in the event a location does not match the first two `Route`s the `IonRouterOutlet` will redirect the Ionic React app to the `/dashboard` path.
107
103
108
104
You can alternatively supply a component to render instead of providing a redirect.
The `IonPage` component wraps each view in an Ionic React app and allows page transitions and stack navigation to work properly. Each view that is navigated to using the router must include an `IonPage` component.
127
119
@@ -186,7 +178,7 @@ Outside of these components that have the `routerLink` prop, you can also use Re
186
178
187
179
We recommend using one of the above methods whenever possible for routing. The advantage to these approaches is that they both render an anchor (`<a>`)tag, which is suitable for overall app accessibility.
188
180
189
-
For programmatic navigation, use the `useIonRouter` hook (see [Utilities](#useionrouter)) or React Router's [`useNavigate`](https://reactrouter.com/6.28.0/hooks/use-navigate) hook:
181
+
For programmatic navigation, use the `useIonRouter` hook (review [Utility Functions](./utility-functions.md#useionrouter)) or React Router's [`useNavigate`](https://reactrouter.com/6.28.0/hooks/use-navigate) hook:
190
182
191
183
```tsx
192
184
import { useNavigate } from'react-router-dom';
@@ -217,7 +209,7 @@ Say you have the following application history:
217
209
218
210
If you were to call `navigate(-2)` on `/pageC`, you would be brought back to `/pageA`. If you then called `navigate(2)`, you would be brought to `/pageC`.
219
211
220
-
Using `navigate()` with delta values is not recommended in Ionic React because it follows the browser's linear history, which does not account for Ionic's non-linear tab and nested outlet navigation stacks. Use the `useIonRouter` hook's [`goBack()`](#useionrouter) method instead, which navigates within the current Ionic navigation stack.
212
+
Using `navigate()` with delta values is not recommended in Ionic React because it follows the browser's linear history, which does not account for Ionic's non-linear tab and nested outlet navigation stacks. Use the `useIonRouter` hook's [`goBack()`](./utility-functions.md#back-navigation) method instead, which navigates within the current Ionic navigation stack.
221
213
222
214
## URL Parameters
223
215
@@ -308,9 +300,9 @@ We recommend keeping your application as simple as possible until you need to ad
308
300
309
301
The two most common uses of non-linear routing is with tabs and nested `IonRouterOutlets`. We recommend only using non-linear routing if your application meets the tabs or nested router outlet use cases.
310
302
311
-
For more on tabs, please see[Working with Tabs](#working-with-tabs).
303
+
For more on tabs, refer to[Working with Tabs](#working-with-tabs).
312
304
313
-
For more on nested router outlets, please see[Nested Routes](#nested-routes).
305
+
For more on nested router outlets, refer to[Nested Routes](#nested-routes).
@@ -509,7 +499,9 @@ The example below shows how the Spotify app reuses the same album component to s
509
499
510
500
## Live Example
511
501
512
-
If you would prefer to get hands on with the concepts and code described above, please checkout our [live example](https://stackblitz.com/edit/ionic-react-routing?file=src/index.tsx) of the topics above on StackBlitz.
502
+
import NavigationPlayground from '@site/static/usage/v9/navigation/index.md';
503
+
504
+
<NavigationPlaygrounddefaultFramework="react" />
513
505
514
506
### IonRouterOutlet in a Tabs View
515
507
@@ -527,63 +519,8 @@ For example, the routes for a view with two tabs (sessions and speakers) can be
527
519
528
520
When a user navigates to a session detail page ("/sessions/1" for instance), `IonRouterOutlet` sees that both the list and detail pages share the same "sessions" path prefix and provides an animated page transition to the new view. If a user navigates to a different tab ("speakers" in this case), `IonRouterOutlet` knows not to provide the animation.
529
521
530
-
## Utilities
531
-
532
-
### useIonRouter
533
-
534
-
The `useIonRouter` hook gives you more control over routing in Ionic React, including custom page transition animations and Ionic-aware back navigation via `goBack()`.
535
-
536
-
The `useIonRouter` hook returns a `UseIonRouterResult` which has several convenience methods for routing:
537
-
538
-
```typescript
539
-
typeUseIonRouterResult= {
540
-
/**
541
-
* Navigates to a new pathname
542
-
* @parampathname - The path to navigate to
543
-
* @paramrouterDirection - Optional - The RouterDirection to use for transition purposes, defaults to 'forward'
544
-
* @paramrouteAction - Optional - The RouteAction to use for history purposes, defaults to 'push'
545
-
* @paramrouterOptions - Optional - Any additional parameters to pass to the router
546
-
* @paramanimationBuilder - Optional - A custom transition animation to use
547
-
*/
548
-
push(
549
-
pathname:string,
550
-
routerDirection?:RouterDirection,
551
-
routeAction?:RouteAction,
552
-
routerOptions?:RouterOptions,
553
-
animationBuilder?:AnimationBuilder
554
-
):void;
555
-
/**
556
-
* Navigates backwards in history, using the IonRouter to determine history
557
-
* @paramanimationBuilder - Optional - A custom transition animation to use
558
-
*/
559
-
goBack(animationBuilder?:AnimationBuilder):void;
560
-
/**
561
-
* Determines if there are any additional routes in the Router's history. However, routing is not prevented if the browser's history has more entries. Returns true if more entries exist, false if not.
562
-
*/
563
-
canGoBack():boolean;
564
-
/**
565
-
* Information about the current route.
566
-
*/
567
-
routeInfo:RouteInfo;
568
-
};
569
-
```
570
-
571
-
The following example shows how to use `useIonRouter`:
572
-
573
-
```tsx
574
-
import { useIonRouter } from'@ionic/react';
575
-
576
-
const MyComponent:React.FC= () => {
577
-
const router =useIonRouter();
578
-
const goToPage = () => {
579
-
router.push('/my-page', 'root', 'replace');
580
-
};
581
-
582
-
...
583
-
}
584
-
585
-
```
586
-
587
522
## More Information
588
523
589
524
For more info on routing in React using the React Router implementation that Ionic uses under the hood, check out their docs at [https://reactrouter.com/6.28.0](https://reactrouter.com/6.28.0).
525
+
526
+
For documentation on `useIonRouter` and other utility functions, review [Utility Functions](./utility-functions.md).
content="Ionic React utility functions including useIonRouter for programmatic navigation with custom transitions, Ionic-aware back navigation, and routing history control."
11
+
/>
12
+
</head>
13
+
14
+
Ionic React provides utility functions for common tasks like programmatic navigation and controlling page transitions.
Returns the Ionic router instance, which provides methods for programmatic navigation with control over page transitions. Use this hook instead of React Router's `useNavigate` when you need to customize the transition animation or use Ionic-aware back navigation.
The `goBack()` method navigates within the current Ionic navigation stack, unlike React Router's `navigate(-1)` which follows the browser's linear history.
50
+
51
+
```tsx
52
+
import { useIonRouter } from'@ionic/react';
53
+
54
+
const MyComponent:React.FC= () => {
55
+
const router =useIonRouter();
56
+
57
+
const handleBack = () => {
58
+
if (router.canGoBack()) {
59
+
router.goBack();
60
+
}
61
+
};
62
+
63
+
...
64
+
};
65
+
```
66
+
67
+
##### canGoBack
68
+
69
+
Use `canGoBack()` to check whether there are additional routes in the Ionic router's history. This is useful when deciding whether to show a back button or handle the hardware back button on Android.
70
+
71
+
```tsx
72
+
import { useIonRouter } from'@ionic/react';
73
+
74
+
const MyComponent:React.FC= () => {
75
+
const router =useIonRouter();
76
+
77
+
// Returns true if more entries exist in Ionic's history stack
78
+
const hasHistory =router.canGoBack();
79
+
80
+
...
81
+
};
82
+
```
83
+
84
+
##### navigateRoot
85
+
86
+
Use `navigateRoot()` to navigate to a new root pathname, clearing the navigation history and unmounting all previous views. After navigation, `canGoBack()` will return `false`. This is useful for navigating to a new root after login or logout.
87
+
88
+
```tsx
89
+
import { useIonRouter } from'@ionic/react';
90
+
91
+
const MyComponent:React.FC= () => {
92
+
const router =useIonRouter();
93
+
94
+
const handleLogout = () => {
95
+
router.navigateRoot('/login');
96
+
};
97
+
98
+
...
99
+
};
100
+
```
101
+
102
+
Review the [React Navigation Documentation](./navigation.md) for more navigation examples.
* Determines if there are any additional routes in the Router's history. However, routing is not prevented if the browser's history has more entries. Returns true if more entries exist, false if not.
141
+
*/
142
+
canGoBack():boolean;
143
+
/**
144
+
* Information about the current route.
145
+
*/
146
+
routeInfo:RouteInfo;
147
+
/**
148
+
* @deprecated Use goBack instead.
149
+
* @paramanimationBuilder - Optional - A custom transition animation to use
0 commit comments