Navigator


目录

Navigator is a collection components that allow rendering nested views/panels/menus (via the Navigator.Screen component) and navigate between them (via the Navigator.Button and Navigator.BackButton components).

Usage

import { Navigator } from '@wordpress/components';

const MyNavigation = () => (
    <Navigator initialPath="/">
        <Navigator.Screen path="/">
            <p>This is the home screen.</p>
            <Navigator.Button path="/child">
                Navigate to child screen.
            </Navigator.Button>
        </Navigator.Screen>
        <Navigator.Screen path="/child">
            <p>This is the child screen.</p>
            <Navigator.BackButton>Go back</Navigator.BackButton>
        </Navigator.Screen>
    </Navigator>
);

Hierarchical paths

Navigator assumes that screens are organized hierarchically according to their path, which should follow a URL-like scheme where each path segment starts with and is separated by the / character.

Navigator will treat “back” navigations as going to the parent screen — it is, therefore, the responsibility of the consumer of the component to create the correct screen hierarchy.

For example:

Height and animations

Due to how Navigator.Screen animations work, it is recommended that the Navigator component is assigned a height to prevent some potential UI jumps while moving across screens.

Individual components

Navigator is comprised of four individual components:

For advanced usages, consumers can use the useNavigator hook.

Navigator

Props
initialPath: string

The initial active path.

children: string

The children elements.

Navigator.Screen

Props
path: string

The screen’s path, matched against the current path stored in the navigator.

Navigator assumes that screens are organized hierarchically according to their path, which should follow a URL-like scheme where each path segment starts with and is separated by the / character.

Navigator will treat “back” navigations as going to the parent screen — it is, therefore, the responsibility of the consumer of the component to create the correct screen hierarchy.

For example:

children: string

The children elements.

Navigator.Button

Props
path: string

The path of the screen to navigate to. The value of this prop needs to be a valid value for an HTML attribute.

attributeName: string

The HTML attribute used to identify the Navigator.Button, which is used by Navigator to restore focus.

children: string

The children elements.

Inherited props

Navigator.Button also inherits all of the Button props, except for href and target.

Navigator.BackButton

Props
children: string

The children elements.

Inherited props

Navigator.BackButton also inherits all of the Button props, except for href and target.

useNavigator

You can retrieve a navigator instance by using the useNavigator hook.

Props

The navigator instance has a few properties:

goTo: ( path: string, options: NavigateOptions ) => void

The goTo function allows navigating to a given path. The second argument can augment the navigation operations with different options.

The available options are:

goBack: ( path: string, options: NavigateOptions ) => void

The goBack function allows navigating to the parent screen. Parent/child navigation only works if the paths you define are hierarchical (see note above).

When a match is not found, the function will try to recursively navigate the path hierarchy until a matching screen (or the root /) is found.

The available options are the same as for the goTo method, except for the isBack property, which is not available for the goBack method.

location: NavigatorLocation

The location object represents the current location, and has a few properties:

params: Record< string, string | string[] >

The parsed record of parameters from the current location. For example if the current screen path is /product/:productId and the location is /product/123, then params will be { productId: '123' }.


版本号 #2
由 壳壳虫 创建于 2026-10-31 18:16:33 UTC
由 壳壳虫 更新于 2026-10-31 18:16:34 UTC