Skip to main content
Version: 6.x

导航状态参考

导航状态是 React Navigation 存储应用的导航结构和历史记录的状态。如果你需要执行 重置状态提供自定义初始状态 等高级操作,了解导航状态的结构很有用。

¥The navigation state is the state where React Navigation stores the navigation structure and history of the app. It's useful to know about the structure of the navigation state if you need to do advanced operations such as resetting the state, providing a custom initial state etc.

它是一个 JavaScript 对象,如下所示:

¥It's a JavaScript object which looks like this:

const state = {
type: 'stack',
key: 'stack-1',
routeNames: ['Home', 'Profile', 'Settings'],
routes: [
{ key: 'home-1', name: 'Home', params: { sortBy: 'latest' } },
{ key: 'settings-1', name: 'Settings' },
],
index: 1,
stale: false,
};

每个导航状态对象中都存在一些属性:

¥There are few properties present in every navigation state object:

  • type - 状态所属导航器的类型,例如 stacktabdrawer

    ¥type - Type of the navigator that the state belongs to, e.g. stack, tab, drawer.

  • key - 用于识别导航器的唯一密钥。

    ¥key - Unique key to identify the navigator.

  • routeNames - 导航器中定义的屏幕名称。这是一个包含每个屏幕字符串的唯一数组。

    ¥routeNames - Name of the screens defined in the navigator. This is an unique array containing strings for each screen.

  • routes - 在导航器中呈现的路由对象(屏幕)列表。它还代表堆栈导航器中的历史记录。该数组中应该至少存在一项。

    ¥routes - List of route objects (screens) which are rendered in the navigator. It also represents the history in a stack navigator. There should be at least one item present in this array.

  • index - routes 数组中焦点路由对象的索引。

    ¥index - Index of the focused route object in the routes array.

  • history - 已访问项目的列表。这是一个可选属性,并非所有导航器中都存在。例如,它仅存在于核心的选项卡和抽屉导航器中。history 数组中项目的形状可能因导航器而异。该数组中应该至少存在一项。

    ¥history - A list of visited items. This is an optional property and not present in all navigators. For example, it's only present in tab and drawer navigators in the core. The shape of the items in the history array can vary depending on the navigator. There should be at least one item present in this array.

  • stale - 导航状态被假定为过时的,除非 stale 属性显式设置为 false。这意味着状态对象需要是 "rehydrated"

    ¥stale - A navigation state is assumed to be stale unless the stale property is explicitly set to false. This means that the state object needs to be "rehydrated".

routes 数组中的每个路由对象可能包含以下属性:

¥Each route object in a routes array may contain the following properties:

  • key - 屏幕的唯一键。自动创建或在导航到此屏幕时添加。

    ¥key - Unique key of the screen. Created automatically or added while navigating to this screen.

  • name - 屏幕名称。在导航器组件层次结构中定义。

    ¥name - Name of the screen. Defined in navigator component hierarchy.

  • params - 包含在导航时定义的参数的可选对象,例如 navigate('Home', { sortBy: 'latest' })

    ¥params - An optional object containing params which is defined while navigating e.g. navigate('Home', { sortBy: 'latest' }).

  • state - 一个可选对象,包含嵌套在该屏幕内的子导航器的导航状态。

    ¥state - An optional object containing the navigation state of a child navigator nested inside this screen.

例如,包含嵌套在其主屏幕内的选项卡导航器的堆栈导航器可能具有如下导航状态对象:

¥For example, a stack navigator containing a tab navigator nested inside it's home screen may have a navigation state object like this:

const state = {
type: 'stack',
key: 'stack-1',
routeNames: ['Home', 'Profile', 'Settings'],
routes: [
{
key: 'home-1',
name: 'Home',
state: {
key: 'tab-1',
routeNames: ['Feed', 'Library', 'Favorites'],
routes: [
{ key: 'feed-1', name: 'Feed', params: { sortBy: 'latest' } },
{ key: 'library-1', name: 'Library' },
{ key: 'favorites-1', name: 'Favorites' },
],
index: 0,
},
},
{ key: 'settings-1', name: 'Settings' },
],
index: 1,
};

需要注意的是,即使存在嵌套导航器,在发生导航之前也不会添加 route 对象上的 state 属性,因此不能保证它存在。

¥It's important to note that even if there's a nested navigator, the state property on the route object is not added until a navigation happens, hence it's not guaranteed to exist.

部分状态对象

¥Partial state objects

之前提到过导航状态中的 stale 属性。陈旧的导航状态意味着状态对象在使用之前需要重新水化或修复或固定,例如添加丢失的键、删除无效的屏幕等。作为用户,你无需担心它,React Navigation 将自动修复状态对象中的任何问题,除非 stale 设置为 false。如果你正在编写 定制路由,则 getRehydratedState 方法可让你编写自定义补水逻辑来修复状态对象。

¥Earlier there was a mention of stale property in the navigation state. A stale navigation state means that the state object needs to be rehydrated or fixed or fixed up, such as adding missing keys, removing invalid screens etc. before being used. As a user, you don't need to worry about it, React Navigation will fix up any issues in a state object automatically unless stale is set to false. If you're writing a custom router, the getRehydratedState method let's you write custom rehydration logic to fix up state objects.

这也适用于 index 属性:index 应该是堆栈中的最后一个路由,如果指定了不同的值,React Navigation 会修复它。例如,如果你想重置应用的导航状态以使其显示 Profile 路由,并在返回时显示 Home 路由,请执行以下操作:

¥This also applies to the index property: index should be the last route in a stack, and if a different value was specified, React Navigation fixes it. For example, if you wanted to reset your app's navigation state to have it display the Profile route, and have the Home route displayed upon going back, and did the below,

navigation.reset({
index: 0,
routes: [{ name: 'Home' }, { name: 'Profile' }],
});

React Navigation 会将 index 更正为 1,并显示路由并按预期执行导航。

¥React Navigation would correct index to 1, and display the route and perform navigation as intended.

在执行诸如 reset提供初始状态 等操作时,此功能非常方便,因为你可以安全地省略导航状态对象中的许多属性,并依靠 React Navigation 为你添加这些属性,从而使你的代码更简单。例如,你只能提供一个没有任何键的 routes 数组,React Navigation 将自动添加使其工作所需的所有内容:

¥This feature comes handy when doing operations such as reset, providing a initial state etc., as you can safely omit many properties from the navigation state object and relying on React Navigation to add those properties for you, making your code simpler. For example, you can only provide a routes array without any keys and React Navigation will automatically add everything that's needed to make it work:

const state = {
routes: [{ name: 'Home' }, { name: 'Profile' }],
};

补水后,它会看起来像这样:

¥After rehydration, it'll look something like this:

const state = {
type: 'stack',
key: 'stack-1',
routeNames: ['Home', 'Profile', 'Settings'],
routes: [
{ key: 'home-1', name: 'Home' },
{ key: 'settings-1', name: 'Settings' },
],
index: 1,
stale: false,
};

在这里,React Navigation 填充了缺失的部分,例如键、路由名称、索引等。

¥Here, React Navigation filled in the missing bits such as keys, route names, index etc.

还可以提供无效数据,例如不存在的屏幕,它会自动修复。虽然不建议使用无效的状态对象编写代码,但如果你执行诸如 状态持续性 之类的操作,那么它会非常有用,其中配置的屏幕可能在更新后发生更改,如果 React Navigation 没有自动修复状态对象,这可能会导致问题。

¥It's also possible to provide invalid data such as non-existent screens and it'll be fixed automatically. While it's not recommended to write code with invalid state objects, it can be super useful if you do things like state persistence, where the configured screens might have changed after an update, which could cause problems if React Navigation didn't fix the state object automatically.

提示

如果你希望 React Navigation 修复无效状态,你需要确保状态对象中没有 stale: false。带有 stale: false 的状态对象被假定为有效的状态对象,React Navigation 不会尝试修复它们。

¥If you want React Navigation to fix invalid state, you need to make sure that you don't have stale: false in the state object. State objects with stale: false are assumed to be valid state objects and React Navigation won't attempt to fix them.

当你在 initialState 中提供状态对象时,React Navigation 将始终假定它是过时的状态对象,这确保状态持久性之类的操作顺利进行,而无需对状态对象进行额外操作。

¥When you're providing a state object in initialState, React Navigation will always assume that it's a stale state object, which makes sure that things like state persistence work smoothly without extra manipulation of the state object.