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
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
73
74
-
These routes are grouped in an `IonRouterOutlet`, let's discuss that next.
74
+
These routes are grouped in an `IonRouterOutlet`.
75
75
76
-
## IonRouterOutlet
76
+
## Components
77
+
78
+
### IonRouterOutlet
77
79
78
80
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
81
80
82
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
83
82
84
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
85
84
-
## Fallback Route
86
+
###Fallback Route
85
87
86
88
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.
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
129
@@ -186,7 +188,7 @@ Outside of these components that have the `routerLink` prop, you can also use Re
186
188
187
189
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
190
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:
191
+
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
192
191
193
```tsx
192
194
import { useNavigate } from'react-router-dom';
@@ -217,7 +219,7 @@ Say you have the following application history:
217
219
218
220
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
221
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.
222
+
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
223
222
224
## URL Parameters
223
225
@@ -308,9 +310,9 @@ We recommend keeping your application as simple as possible until you need to ad
308
310
309
311
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
312
311
-
For more on tabs, please see[Working with Tabs](#working-with-tabs).
313
+
For more on tabs, refer to[Working with Tabs](#working-with-tabs).
312
314
313
-
For more on nested router outlets, please see[Nested Routes](#nested-routes).
315
+
For more on nested router outlets, refer to[Nested Routes](#nested-routes).
314
316
315
317
## Shared URLs versus Nested Routes
316
318
@@ -527,63 +529,8 @@ For example, the routes for a view with two tabs (sessions and speakers) can be
527
529
528
530
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
531
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
532
## More Information
588
533
589
534
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).
535
+
536
+
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
Copy file name to clipboardExpand all lines: docs/vue/utility-functions.md
+14-3Lines changed: 14 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,15 @@
1
-
# Utility Functions
1
+
---
2
+
title: Utility Functions
3
+
sidebar_label: Utility Functions
4
+
---
5
+
6
+
<head>
7
+
<title>Ionic Vue Utility Functions</title>
8
+
<meta
9
+
name="description"
10
+
content="Ionic Vue utility functions including useIonRouter for programmatic navigation, useBackButton for hardware back button handling, and useKeyboard for on-screen keyboard state."
11
+
/>
12
+
</head>
2
13
3
14
Ionic Vue ships with several utility functions that you can use in your application to make certain tasks easier such as managing the on-screen keyboard and the hardware back button.
4
15
@@ -12,7 +23,7 @@ Ionic Vue ships with several utility functions that you can use in your applicat
12
23
13
24
Returns the Ionic router instance, containing API methods for navigating, customizing page transitions and routing context for native features. This function can be used in combination with the [`useRouter`](https://router.vuejs.org/api/index.html#userouter) from Vue.
0 commit comments