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/react/navigation.md
+11-64Lines changed: 11 additions & 64 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,15 +71,17 @@ Note the `ionPage` prop on `IonRouterOutlet`. When a component serves as a neste
71
71
72
72
These routes are grouped in an `IonRouterOutlet`, let's discuss that next.
73
73
74
-
## IonRouterOutlet
74
+
## Components
75
+
76
+
### IonRouterOutlet
75
77
76
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.
77
79
78
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.
79
81
80
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`.
81
83
82
-
## Fallback Route
84
+
###Fallback Route
83
85
84
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.
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.
117
119
@@ -176,7 +178,7 @@ Outside of these components that have the `routerLink` prop, you can also use Re
176
178
177
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.
178
180
179
-
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:
180
182
181
183
```tsx
182
184
import { useNavigate } from'react-router-dom';
@@ -207,7 +209,7 @@ Say you have the following application history:
207
209
208
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`.
209
211
210
-
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.
211
213
212
214
## URL Parameters
213
215
@@ -298,9 +300,9 @@ We recommend keeping your application as simple as possible until you need to ad
298
300
299
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.
300
302
301
-
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).
302
304
303
-
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).
304
306
305
307
## Shared URLs versus Nested Routes
306
308
@@ -566,63 +568,8 @@ For example, the routes for a view with two tabs (sessions and speakers) can be
566
568
567
569
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.
568
570
569
-
## Utilities
570
-
571
-
### useIonRouter
572
-
573
-
The `useIonRouter` hook gives you more control over routing in Ionic React, including custom page transition animations and Ionic-aware back navigation via `goBack()`.
574
-
575
-
The `useIonRouter` hook returns a `UseIonRouterResult` which has several convenience methods for routing:
576
-
577
-
```typescript
578
-
typeUseIonRouterResult= {
579
-
/**
580
-
* Navigates to a new pathname
581
-
* @parampathname - The path to navigate to
582
-
* @paramrouterDirection - Optional - The RouterDirection to use for transition purposes, defaults to 'forward'
583
-
* @paramrouteAction - Optional - The RouteAction to use for history purposes, defaults to 'push'
584
-
* @paramrouterOptions - Optional - Any additional parameters to pass to the router
585
-
* @paramanimationBuilder - Optional - A custom transition animation to use
586
-
*/
587
-
push(
588
-
pathname:string,
589
-
routerDirection?:RouterDirection,
590
-
routeAction?:RouteAction,
591
-
routerOptions?:RouterOptions,
592
-
animationBuilder?:AnimationBuilder
593
-
):void;
594
-
/**
595
-
* Navigates backwards in history, using the IonRouter to determine history
596
-
* @paramanimationBuilder - Optional - A custom transition animation to use
597
-
*/
598
-
goBack(animationBuilder?:AnimationBuilder):void;
599
-
/**
600
-
* 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.
601
-
*/
602
-
canGoBack():boolean;
603
-
/**
604
-
* Information about the current route.
605
-
*/
606
-
routeInfo:RouteInfo;
607
-
};
608
-
```
609
-
610
-
The following example shows how to use `useIonRouter`:
611
-
612
-
```tsx
613
-
import { useIonRouter } from'@ionic/react';
614
-
615
-
const MyComponent:React.FC= () => {
616
-
const router =useIonRouter();
617
-
const goToPage = () => {
618
-
router.push('/my-page', 'root', 'replace');
619
-
};
620
-
621
-
...
622
-
}
623
-
624
-
```
625
-
626
571
## More Information
627
572
628
573
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).
574
+
575
+
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