Skip to main content
Version: 6.x

StackActions 参考

StackActions 是一个对象,包含用于生成特定于基于堆栈的导航器的操作的方法。其方法扩展了 CommonActions 中可用的操作。

¥StackActions is an object containing methods for generating actions specific to stack-based navigators. Its methods expand upon the actions available in CommonActions.

支持以下操作:

¥The following actions are supported:

replace

replace 操作允许替换 导航状态 中的路由。它需要以下参数:

¥The replace action allows to replace a route in the navigation state. It takes the following arguments:

  • name - string - 已在某处注册的路由的目的地名称。

    ¥name - string - A destination name of the route that has been registered somewhere.

  • params - object - 要传递到目标路由的参数。

    ¥params - object - Params to pass to the destination route.

import { StackActions } from '@react-navigation/native';

navigation.dispatch(
StackActions.replace('Profile', {
user: 'jane',
})
);

如果要替换特定路由,可以添加引用路由键的 source 属性和引用导航状态键的 target 属性:

¥If you want to replace a particular route, you can add a source property referring to the route key and target property referring to the navigation state key:

import { StackActions } from '@react-navigation/native';

navigation.dispatch({
...StackActions.replace('Profile', {
user: 'jane',
}),
source: route.key,
target: navigation.getState().key,
});

如果 source 属性显式设置为 undefined,它将替换焦点路由。

¥If the source property is explicitly set to undefined, it'll replace the focused route.

push

push 操作在堆栈顶部添加一条路由并向前导航至该路由。这与 navigate 的不同之处在于,如果给定名称的路由已存在,则 navigate 将弹回堆栈中的较早位置。push 将始终添加在顶部,因此一条路由可以出现多次。

¥The push action adds a route on top of the stack and navigates forward to it. This differs from navigate in that navigate will pop back to earlier in the stack if a route of the given name is already present there. push will always add on top, so a route can be present multiple times.

  • name - string - 要推入堆栈的路由名称。

    ¥name - string - Name of the route to push onto the stack.

  • params - object - 屏幕参数传递到目标路由。

    ¥params - object - Screen params to pass to the destination route.

import { StackActions } from '@react-navigation/native';

const pushAction = StackActions.push('Profile', { user: 'Wojtek' });

navigation.dispatch(pushAction);

pop

pop 操作将带你返回到堆栈中的上一个屏幕。它需要一个可选参数 (count),它允许你指定要弹出的屏幕数。

¥The pop action takes you back to a previous screen in the stack. It takes one optional argument (count), which allows you to specify how many screens to pop back by.

import { StackActions } from '@react-navigation/native';

const popAction = StackActions.pop(1);

navigation.dispatch(popAction);

popToTop

popToTop 操作会将你带回到堆栈中的第一个屏幕,忽略所有其他屏幕。它的功能与 StackActions.pop({n: currentIndex}) 相同。

¥The popToTop action takes you back to the first screen in the stack, dismissing all the others. It's functionally identical to StackActions.pop({n: currentIndex}).

import { StackActions } from '@react-navigation/native';

navigation.dispatch(StackActions.popToTop());