将参数从堆栈导航器传递到选项卡导航器(反应导航 5)

Omr*_*Ram 2 javascript react-native redux react-navigation-stack react-navigation-bottom-tab

我试图将用户 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>

)
Run Code Online (Sandbox Code Playgroud)

每当我从登录屏幕转到主页并调用 this.props.navigation.getParam('id') 时,我都会收到一条错误消息,指出它不是一个函数。仔细检查后,我发现 this.props.navigation.state 是未定义的。可能意味着我的参数不会传递到主屏幕,即使我导航到它没有任何问题。

我尝试使用 NavigationActions 但我收到一个错误,我在其他任何地方都没有提到。

我像这样导航到主页:

this.props.navigation.navigate('Home' { id: 'myId' })
Run Code Online (Sandbox Code Playgroud)

我也试过这个语法:

 navigation.navigate('Root', {
  screen: 'Settings',
  params: { user: 'jane' },
});
Run Code Online (Sandbox Code Playgroud)

但我得到了同样的错误

还值得注意的是,我使用了 React.component 而不是将我的屏幕变成函数。我不确定这是否会导致任何问题,因为在 react-navigation v5 教程中,我看到这家伙使用函数而不是 react 组件。

任何帮助将不胜感激!

abd*_*bdi 5

尝试这个:

props.navigation.navigate('mainStack' { params: {id: 'myId'}, screen: 'Home' )

Run Code Online (Sandbox Code Playgroud)

并且在访问而不是使用时getParam使用props.route.params.id