Skip to content

Commit 213e9d8

Browse files
committed
docs(react): add utility functions page with useIonRouter docs
1 parent bee3752 commit 213e9d8

File tree

3 files changed

+156
-59
lines changed

3 files changed

+156
-59
lines changed

docs/react/navigation.md

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Outside of these components that have the `routerLink` prop, you can also use Re
186186

187187
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.
188188

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:
189+
For programmatic navigation, use the `useIonRouter` hook (see [Utility Functions](./utility-functions#useionrouter)) or React Router's [`useNavigate`](https://reactrouter.com/6.28.0/hooks/use-navigate) hook:
190190

191191
```tsx
192192
import { useNavigate } from 'react-router-dom';
@@ -217,7 +217,7 @@ Say you have the following application history:
217217

218218
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`.
219219

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.
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()`](./utility-functions#back-navigation) method instead, which navigates within the current Ionic navigation stack.
221221

222222
## URL Parameters
223223

@@ -527,63 +527,8 @@ For example, the routes for a view with two tabs (sessions and speakers) can be
527527

528528
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.
529529

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-
type UseIonRouterResult = {
540-
/**
541-
* Navigates to a new pathname
542-
* @param pathname - The path to navigate to
543-
* @param routerDirection - Optional - The RouterDirection to use for transition purposes, defaults to 'forward'
544-
* @param routeAction - Optional - The RouteAction to use for history purposes, defaults to 'push'
545-
* @param routerOptions - Optional - Any additional parameters to pass to the router
546-
* @param animationBuilder - 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-
* @param animationBuilder - 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-
587530
## More Information
588531

589532
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).
533+
534+
For documentation on `useIonRouter` and other utility functions, see [Utility Functions](./utility-functions).

docs/react/utility-functions.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
title: Utility Functions
3+
sidebar_label: Utility Functions
4+
---
5+
6+
<head>
7+
<title>Ionic React Utility Functions: useIonRouter Hook</title>
8+
<meta
9+
name="description"
10+
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.
15+
16+
## Router
17+
18+
### useIonRouter
19+
20+
**useIonRouter**(): [`UseIonRouterResult`](#useionrouterresult)
21+
22+
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.
23+
24+
#### Customizing Page Transitions
25+
26+
```tsx
27+
import { useIonRouter } from '@ionic/react';
28+
import { customAnimation } from '../animations/customAnimation';
29+
30+
const MyComponent: React.FC = () => {
31+
const router = useIonRouter();
32+
33+
const goToPage = () => {
34+
router.push('/my-page', 'forward', 'push', undefined, customAnimation);
35+
};
36+
37+
const goBack = () => {
38+
router.goBack(customAnimation);
39+
};
40+
41+
...
42+
};
43+
```
44+
45+
#### Back Navigation
46+
47+
The `goBack()` method navigates within the current Ionic navigation stack, unlike React Router's `navigate(-1)` which follows the browser's linear history.
48+
49+
```tsx
50+
import { useIonRouter } from '@ionic/react';
51+
52+
const MyComponent: React.FC = () => {
53+
const router = useIonRouter();
54+
55+
const handleBack = () => {
56+
if (router.canGoBack()) {
57+
router.goBack();
58+
}
59+
};
60+
61+
...
62+
};
63+
```
64+
65+
#### canGoBack
66+
67+
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.
68+
69+
```tsx
70+
import { useIonRouter } from '@ionic/react';
71+
72+
const MyComponent: React.FC = () => {
73+
const router = useIonRouter();
74+
75+
// Returns true if more entries exist in Ionic's history stack
76+
const hasHistory = router.canGoBack();
77+
78+
...
79+
};
80+
```
81+
82+
#### navigateRoot
83+
84+
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.
85+
86+
```tsx
87+
import { useIonRouter } from '@ionic/react';
88+
89+
const MyComponent: React.FC = () => {
90+
const router = useIonRouter();
91+
92+
const handleLogout = () => {
93+
router.navigateRoot('/login');
94+
};
95+
96+
...
97+
};
98+
```
99+
100+
See the [React Navigation Documentation](./navigation) for more navigation examples.
101+
102+
### Interfaces
103+
104+
#### UseIonRouterResult
105+
106+
```typescript
107+
import { AnimationBuilder, RouterDirection, RouteAction, RouterOptions, RouteInfo } from '@ionic/react';
108+
109+
type UseIonRouterResult = {
110+
/**
111+
* Navigates to a new pathname
112+
* @param pathname - The path to navigate to
113+
* @param routerDirection - Optional - The RouterDirection to use for transition purposes, defaults to 'forward'
114+
* @param routeAction - Optional - The RouteAction to use for history purposes, defaults to 'push'
115+
* @param routerOptions - Optional - Any additional parameters to pass to the router
116+
* @param animationBuilder - Optional - A custom transition animation to use
117+
*/
118+
push(
119+
pathname: string,
120+
routerDirection?: RouterDirection,
121+
routeAction?: RouteAction,
122+
routerOptions?: RouterOptions,
123+
animationBuilder?: AnimationBuilder
124+
): void;
125+
/**
126+
* Navigates backwards in history, using the IonRouter to determine history
127+
* @param animationBuilder - Optional - A custom transition animation to use
128+
*/
129+
goBack(animationBuilder?: AnimationBuilder): void;
130+
/**
131+
* Navigates to a new root pathname, clearing the navigation history and unmounting all previous views.
132+
* After navigation, canGoBack() will return false. Useful for navigating to a new root after login/logout.
133+
* @param pathname - The path to navigate to
134+
* @param animationBuilder - Optional - A custom transition animation to use
135+
*/
136+
navigateRoot(pathname: string, animationBuilder?: AnimationBuilder): void;
137+
/**
138+
* 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.
139+
*/
140+
canGoBack(): boolean;
141+
/**
142+
* Information about the current route.
143+
*/
144+
routeInfo: RouteInfo;
145+
/**
146+
* @deprecated Use goBack instead.
147+
* @param animationBuilder - Optional - A custom transition animation to use
148+
*/
149+
back(animationBuilder?: AnimationBuilder): void;
150+
};
151+
```

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ module.exports = {
127127
'react/add-to-existing',
128128
'react/lifecycle',
129129
'react/navigation',
130+
'react/utility-functions',
130131
'react/virtual-scroll',
131132
'react/slides',
132133
'react/platform',

0 commit comments

Comments
 (0)