-
Notifications
You must be signed in to change notification settings - Fork 3.2k
docs(react): add utility functions page with useIonRouter docs #4455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+180
−68
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
213e9d8
docs(react): add utility functions page with useIonRouter docs
ShaneK 8f1dd14
fix(sidebars): move react utility-functions to match vue sidebar orde…
ShaneK 3ca3145
fix(docs): add .md extensions to internal links and use inclusive lan…
ShaneK 97f180b
fix(react): shorten page title and match vue header structure (Router…
ShaneK 0044e63
fix(vue): add head tag and match header structure with react utility-…
ShaneK c736ef2
fix(react): group IonRouterOutlet, Fallback Route, IonPage under Comp…
ShaneK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
thetaPC marked this conversation as resolved.
Show resolved
Hide resolved
thetaPC marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| --- | ||
| title: Utility Functions | ||
| sidebar_label: Utility Functions | ||
| --- | ||
|
|
||
| <head> | ||
| <title>Ionic React Utility Functions</title> | ||
| <meta | ||
| name="description" | ||
| content="Ionic React utility functions including useIonRouter for programmatic navigation with custom transitions, Ionic-aware back navigation, and routing history control." | ||
| /> | ||
| </head> | ||
|
|
||
| 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; | ||
| }; | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.