传递导航时无法获取参数

问题描述 投票:0回答:1

我有两个导航器,一个Auth是一个StackNavigator包含SignInScreen,一个App是一个BottomTabNavigator包含HomeScreen,我想从HomeScreen导航到SignInScreen,然后通过一些参数来指示来源。

我尝试了很多类似以下的代码。

navigation.navigate('Auth', { source: 'App' })

navigation.navigate(
  'Auth',
  {},
  {
    type: 'Navigate',
    routeName: 'Auth',
    params: { source: 'App' }
  }
)

navigation.navigate(
  'Auth',
  {},
  {
    type: 'Navigate',
    routeName: 'Auth',
    action: {
      type: 'Navigate',
      routeName: 'SignInScreen',
      params: { source: 'App' }
    }
  }
)

...

但是它们都不起作用。 navigation.getParam('source', null)始终返回null。

查看更多https://github.com/liudonghua123/expo-multi-screen-starter/blob/navigation_params_null/src/screens/HomeScreen.js#L22https://github.com/liudonghua123/expo-multi-screen-starter/blob/navigation_params_null/src/screens/SignInScreen.js#L26

我也阅读了https://github.com/react-navigation/react-navigation/issues/143https://github.com/react-navigation/react-navigation/issues/1237

预期行为

  • 我希望代码navigation.getParam('source', null)返回实际参数。

如何复制

我也在https://github.com/react-navigation/react-navigation/issues/6335上发布了它。

javascript reactjs react-native react-navigation
1个回答
0
投票

最后,我需要将代码还原为navigation.navigate('Auth', { source: 'App' })中的HomeScreen,然后可以获得类似以下代码的参数。

    const parent = navigation.dangerouslyGetParent();
    let source = null;
    if (parent && parent.state && parent.state.params) {
      ({ source } = parent.state.params);
    }
© www.soinside.com 2019 - 2024. All rights reserved.