我正在尝试将用户 ID 从登录屏幕传递到作为嵌套选项卡导航器一部分的主屏幕。这是我的应用程序的结构:
const mainStack = () =>(
<Tab.Navigator
tabBarOptions={{
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
}}
>
<Tab.Screen name="Home" component={Homescreen}
options = {{headerStyle: {backgroundColor: 'yellow'}}}
/>
<Tab.Screen name="Profile" component={otherStack} />
</Tab.Navigator>
)
export default () => (
<NavigationContainer >
<Stack.Navigator screenOptions={{
headerShown: false
}}>
<Stack.Screen name='Welcome' component={WelcomeScreen} />
<Stack.Screen name = 'Register' component = {RegisterScreen} />
<Stack.Screen name= 'Login' component={LoginScreen} />
<Stack.Screen name = 'mainStack' component={mainStack} />
</Stack.Navigator>
</NavigationContainer>
)
每当我从登录屏幕转到主页并调用 this.props.navigation.getParam('id') 时,我都会收到一条错误消息,指出它不是一个函数。经过仔细检查,我发现 this.props.navigation.state 是未定义的。可能意味着我的参数没有传递到主屏幕,即使我在导航到它时没有任何问题。
我尝试使用 NavigationActions,但我收到一个在其他任何地方都没有提到的错误。
我像这样导航到主页:
this.props.navigation.navigate('Home' { id: 'myId' })
我也试过这个语法:
navigation.navigate('Root', {
screen: 'Settings',
params: { user: 'jane' },
});
但我得到同样的错误
还值得注意的是,我使用了 React.component 而不是将屏幕变成函数。我不确定这是否会导致任何问题,因为在 react-navigation v5 的教程中,我看到这个人使用函数而不是反应组件。
任何帮助将不胜感激!