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.
- Static
- Dynamic
navigation.dispatch(
StackActions.replace('Profile', { user: 'Wojtek' })
);
navigation.dispatch(
StackActions.replace('Profile', { user: 'Wojtek' })
);
如果要替换特定路由,可以添加引用路由键的 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.
- Static
- Dynamic
navigation.dispatch(StackActions.push('Profile', { user: 'Wojtek' }));
navigation.dispatch(StackActions.push('Profile', { user: 'Wojtek' }));
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.
- Static
- Dynamic
navigation.dispatch(StackActions.pop(1));
navigation.dispatch(StackActions.pop(1));
popTo
popTo
操作通过名称将你带回到堆栈中的上一个屏幕。它还允许你将参数传递给路由。
¥The popTo
action takes you back to a previous screen in the stack by the name. It also allows you to pass params to the route.
如果在堆栈中找不到匹配的屏幕,这将弹出当前屏幕并添加具有指定名称和参数的新屏幕 - 本质上表现得像 replace
。这可确保如果上一个具有该名称的屏幕不存在,应用不会中断 - 当从深层链接或推送通知打开屏幕时,或者在网络上使用时,可能会发生这种情况。
¥If a matching screen is not found in the stack, this will pop the current screen and add a new screen with the specified name and params - essentially behaving like a replace
. This ensures that the app doesn't break if a previous screen with the name did not exist - which can happen when the screen was opened from a deep link or push notification, or when used on the web etc.
该方法接受以下参数:
¥The method accepts the following arguments:
-
name
- string - 要导航到的路由的名称。¥
name
- string - Name of the route to navigate to. -
params
- object - 屏幕参数传递到目标路由。¥
params
- object - Screen params to pass to the destination route.
import { StackActions } from '@react-navigation/native';
const popToAction = StackActions.popTo('Profile', { user: 'jane' });
navigation.dispatch(popToAction);
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})
.
- Static
- Dynamic
navigation.dispatch(StackActions.popToTop());
navigation.dispatch(StackActions.popToTop());