diff --git a/docs/react/navigation.md b/docs/react/navigation.md index 1821d0d2977..af1270a7298 100644 --- a/docs/react/navigation.md +++ b/docs/react/navigation.md @@ -71,9 +71,11 @@ const DashboardPage: React.FC = () => { 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. -These routes are grouped in an `IonRouterOutlet`, let's discuss that next. +These routes are grouped in an `IonRouterOutlet`. -## IonRouterOutlet +## Components + +### IonRouterOutlet 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. @@ -81,7 +83,7 @@ The `DashboardPage` above shows a users list page and a details page. When navig 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`. -## Fallback Route +### Fallback Route 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. @@ -121,7 +123,7 @@ const DashboardPage: React.FC = () => { }; ``` -## IonPage +### IonPage 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. @@ -186,7 +188,7 @@ Outside of these components that have the `routerLink` prop, you can also use Re We recommend using one of the above methods whenever possible for routing. The advantage to these approaches is that they both render an anchor (``)tag, which is suitable for overall app accessibility. -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: +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: ```tsx import { useNavigate } from 'react-router-dom'; @@ -217,7 +219,7 @@ Say you have the following application history: 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`. -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. +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. ## URL Parameters @@ -308,9 +310,9 @@ We recommend keeping your application as simple as possible until you need to ad 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. -For more on tabs, please see [Working with Tabs](#working-with-tabs). +For more on tabs, refer to [Working with Tabs](#working-with-tabs). -For more on nested router outlets, please see [Nested Routes](#nested-routes). +For more on nested router outlets, refer to [Nested Routes](#nested-routes). ## Shared URLs versus Nested Routes @@ -527,63 +529,8 @@ For example, the routes for a view with two tabs (sessions and speakers) can be 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. -## Utilities - -### useIonRouter - -The `useIonRouter` hook gives you more control over routing in Ionic React, including custom page transition animations and Ionic-aware back navigation via `goBack()`. - -The `useIonRouter` hook returns a `UseIonRouterResult` which has several convenience methods for routing: - -```typescript -type UseIonRouterResult = { - /** - * Navigates to a new pathname - * @param pathname - The path to navigate to - * @param routerDirection - Optional - The RouterDirection to use for transition purposes, defaults to 'forward' - * @param routeAction - Optional - The RouteAction to use for history purposes, defaults to 'push' - * @param routerOptions - Optional - Any additional parameters to pass to the router - * @param animationBuilder - Optional - A custom transition animation to use - */ - push( - pathname: string, - routerDirection?: RouterDirection, - routeAction?: RouteAction, - routerOptions?: RouterOptions, - animationBuilder?: AnimationBuilder - ): void; - /** - * Navigates backwards in history, using the IonRouter to determine history - * @param animationBuilder - Optional - A custom transition animation to use - */ - goBack(animationBuilder?: AnimationBuilder): void; - /** - * 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. - */ - canGoBack(): boolean; - /** - * Information about the current route. - */ - routeInfo: RouteInfo; -}; -``` - -The following example shows how to use `useIonRouter`: - -```tsx -import { useIonRouter } from '@ionic/react'; - -const MyComponent: React.FC = () => { - const router = useIonRouter(); - const goToPage = () => { - router.push('/my-page', 'root', 'replace'); - }; - - ... -} - -``` - ## More Information 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). + +For documentation on `useIonRouter` and other utility functions, review [Utility Functions](./utility-functions.md). diff --git a/docs/react/utility-functions.md b/docs/react/utility-functions.md new file mode 100644 index 00000000000..ed8814188b8 --- /dev/null +++ b/docs/react/utility-functions.md @@ -0,0 +1,153 @@ +--- +title: Utility Functions +sidebar_label: Utility Functions +--- + + + Ionic React Utility Functions + + + +Ionic React provides utility functions for common tasks like programmatic navigation and controlling page transitions. + +## Router + +### Functions + +#### useIonRouter + +▸ **useIonRouter**(): [`UseIonRouterResult`](#useionrouterresult) + +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. + +##### Customizing Page Transitions + +```tsx +import { useIonRouter } from '@ionic/react'; +import { customAnimation } from '../animations/customAnimation'; + +const MyComponent: React.FC = () => { + const router = useIonRouter(); + + const goToPage = () => { + router.push('/my-page', 'forward', 'push', undefined, customAnimation); + }; + + const goBack = () => { + router.goBack(customAnimation); + }; + + ... +}; +``` + +##### 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. + +```tsx +import { useIonRouter } from '@ionic/react'; + +const MyComponent: React.FC = () => { + const router = useIonRouter(); + + const handleBack = () => { + if (router.canGoBack()) { + router.goBack(); + } + }; + + ... +}; +``` + +##### canGoBack + +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. + +```tsx +import { useIonRouter } from '@ionic/react'; + +const MyComponent: React.FC = () => { + const router = useIonRouter(); + + // Returns true if more entries exist in Ionic's history stack + const hasHistory = router.canGoBack(); + + ... +}; +``` + +##### navigateRoot + +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. + +```tsx +import { useIonRouter } from '@ionic/react'; + +const MyComponent: React.FC = () => { + const router = useIonRouter(); + + const handleLogout = () => { + router.navigateRoot('/login'); + }; + + ... +}; +``` + +Review the [React Navigation Documentation](./navigation.md) for more navigation examples. + +### Interfaces + +#### UseIonRouterResult + +```typescript +import { AnimationBuilder, RouterDirection, RouteAction, RouterOptions, RouteInfo } from '@ionic/react'; + +type UseIonRouterResult = { + /** + * Navigates to a new pathname + * @param pathname - The path to navigate to + * @param routerDirection - Optional - The RouterDirection to use for transition purposes, defaults to 'forward' + * @param routeAction - Optional - The RouteAction to use for history purposes, defaults to 'push' + * @param routerOptions - Optional - Any additional parameters to pass to the router + * @param animationBuilder - Optional - A custom transition animation to use + */ + push( + pathname: string, + routerDirection?: RouterDirection, + routeAction?: RouteAction, + routerOptions?: RouterOptions, + animationBuilder?: AnimationBuilder + ): void; + /** + * Navigates backwards in history, using the IonRouter to determine history + * @param animationBuilder - Optional - A custom transition animation to use + */ + goBack(animationBuilder?: AnimationBuilder): void; + /** + * Navigates to a new root pathname, clearing the navigation history and unmounting all previous views. + * After navigation, canGoBack() will return false. Useful for navigating to a new root after login/logout. + * @param pathname - The path to navigate to + * @param animationBuilder - Optional - A custom transition animation to use + */ + navigateRoot(pathname: string, animationBuilder?: AnimationBuilder): void; + /** + * 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. + */ + canGoBack(): boolean; + /** + * Information about the current route. + */ + routeInfo: RouteInfo; + /** + * @deprecated Use goBack instead. + * @param animationBuilder - Optional - A custom transition animation to use + */ + back(animationBuilder?: AnimationBuilder): void; +}; +``` diff --git a/docs/vue/utility-functions.md b/docs/vue/utility-functions.md index 6d175830679..2c3b7e71fa8 100644 --- a/docs/vue/utility-functions.md +++ b/docs/vue/utility-functions.md @@ -1,4 +1,15 @@ -# Utility Functions +--- +title: Utility Functions +sidebar_label: Utility Functions +--- + + + Ionic Vue Utility Functions + + 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. @@ -12,7 +23,7 @@ Ionic Vue ships with several utility functions that you can use in your applicat 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. -**Customizing Page Transitions** +##### Customizing Page Transitions ```js import { IonPage, useIonRouter } from '@ionic/vue'; @@ -27,7 +38,7 @@ const back = () => { }; ``` -**Hardware back button on Android** +##### Back Navigation You may want to know if you are at the root page of the application when a user presses the hardware back button on Android. diff --git a/sidebars.js b/sidebars.js index 4fd52f82faa..aafb6c47ff3 100644 --- a/sidebars.js +++ b/sidebars.js @@ -129,6 +129,7 @@ module.exports = { 'react/navigation', 'react/virtual-scroll', 'react/slides', + 'react/utility-functions', 'react/platform', 'react/pwa', 'react/overlays',