【发布时间】:2021-03-24 17:13:44
【问题描述】:
我正在通过React-navigation reset actions from their docs
他们在哪里共享了如下所示的 sn-p 示例
import { CommonActions } from '@react-navigation/native';
navigation.dispatch(
CommonActions.reset({
index: 1,
routes: [
{ name: 'Home' },
{
name: 'Profile',
params: { user: 'jane' },
},
],
})
);
从她那里我无法弄清楚 index 是做什么的?
我浏览了react-navigation docs 的状态对象,他们在其中写了这个
index - Index of the focused route object in the routes array
所以我在expo snack上查看了他们的示例
并将重置功能更改为此
<Button
title="Reset navigation state"
onPress={() =>
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [
{
name: 'Profile',
params: { user: 'Cyberduck', key: route.params.key },
},
{ name: 'Home' },
],
})
)
}
/>
在上面我已将索引更改为 0 并将 params: { user: 更改为 'Cyberduck'。我希望当我单击它时,它会将名称从 jane 更改为 Cyberduck,但它会将我导航到 Home 屏幕,即使我的索引为 0 并且我的路线数组中的第一个对象是 Profile 屏幕。
如果我删除 { name: 'Home' }, 对象,那么它会将名称从 Jane 更改为 Cyberduck
有人可以解释一下 react-navigation 中的索引吗?
【问题讨论】:
标签: react-native react-navigation