首页 > 解决方案 > React Navigation 5,未定义的参数

问题描述

我有一个 RootNavigator(堆栈),里面有一个 TabNavigator。在 TabNavigator 中有多个 Screens,包括一个 Stack Navigator,其中有一个名为“Profile”的屏幕。

我想要做的只是将一个参数从 RootNavigator 的屏幕发送到 Profile。

根导航器的屏幕

// Navigate to the profile screen
navigation.navigate("Profile", {
  isUploadingContent: true,
});

它可以正确导航到屏幕,但是当我获得路由参数时,它们是未定义的。

Profile.js

  useEffect(() => {
    // Check if content is uploading...
    if (props.route.params.isUploadingContent) {
      toastRef.current.show(
        "Your photo will be available in a few seconds",
        3500
      );
    }
  }, []);

我也尝试按如下方式发送参数,但得到相同的结果。

根导航器的屏幕

// Navigate to the profile screen (nested navigator)   

navigation.navigate("TabNavigator", {
  screen: "Profile",
  params: {
    isUploadingContent: true,
  },
});

关于这里出了什么问题的任何想法?谢谢。

标签: javascriptreactjsreact-nativeexporeact-navigation

解决方案


好的,我找到了解决方案。它像是

navigation.navigate("TabNavigator", {
  screen: "Profile",
  params: {
    isUploadingContent: true,
  },
});

但是,在最低级别,我有一个名为“Profile”的堆栈导航器,其中还有其他相同的屏幕,我不得不用“Profile”替换“TabNavigator”并且工作正常。

navigation.navigate("Profile", {
  screen: "Profile",
  params: {
    isUploadingContent: true,
  },
});

推荐阅读