Utility Functions
Ionic React は、プログラム的なナビゲーションやページの遷移制御といった、一般的なタスクを実行するためのユーティリティ関数を提供します。
Router
Functions
useIonRouter
▸ useIonRouter(): UseIonRouterResult
Ionic のルーターインスタンスを返します。ページ遷移を制御しながらコードからナビゲーションを実行するためのメソッドが含まれています。遷移アニメーションをカスタマイズする場合や Ionic のナビゲーション履歴に沿って戻る場合は、React Router の useNavigate の代わりにこの hook を使います。
Customizing Page Transitions
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
goBack()メソッドは、現在の Ionic ナビゲーションスタック内で移動します。一方、React Router のnavigate(-1)メソッドは、ブラウザの線形な履歴に従って移動します。
import { useIonRouter } from '@ionic/react';
const MyComponent: React.FC = () => {
const router = useIonRouter();
const handleBack = () => {
if (router.canGoBack()) {
router.goBack();
}
};
...
};
canGoBack
canGoBack() を使うと、Ionic router の履歴に戻り先があるか確認できます。戻るボタンを表示するか、Android のハードウェアの戻るボタンを処理するかを判断するときに便利です。
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
navigateRoot() で新しいルートパスに移動すると、ナビゲーション履歴が消去され、それまでのビューはすべてアンマウントされます。移動後、canGoBack() は false を返します。ログイン後やログアウト後に新しいルートへ移動するときに便利です。
import { useIonRouter } from '@ionic/react';
const MyComponent: React.FC = () => {
const router = useIonRouter();
const handleLogout = () => {
router.navigateRoot('/login');
};
...
};
より多くのナビゲーションの例については、React Navigation のドキュメントを参照してください。
Interfaces
UseIonRouterResult
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;
};