React Navigation是否可以使用哈希URL?

huangapple go评论66阅读模式
英文:

Can I make react-navigation use a hash url?

问题

默认情况下,react-navigation在导航时形成正常的URL路径。这意味着我必须配置我的服务器,以确保我的应用程序使用的路径仅路由到我的应用程序包,这在某种程度上是有限制的,并且需要在托管方面考虑特殊情况。

我想要做的是像react-router的HashRouter一样配置它,其中特定路由位于一个哈希后面,就像:

https://example.com/#/path/to/route

我在react-navigation的文档中没有看到描述这种模式的内容。

我尝试在我的顶级路径中放置一个#,但它似乎试图帮我忙,通过将其变成%23来保护URL中的#,这样做就失去了意义。

我是不是漏掉了什么?

英文:

By default, react-navigation forms normal url paths when navigating. This means I have to configure my server to make sure paths that my app uses are routed only to my app bundle, which is kind of limiting and requires special considerations in hosting.

What I'd like to do is configure it like react-router's HashRouter, where the specific route is after a hash, like:

https://example.com/#/path/to/route

I see no documentation in react-navigation describing a mode like this.

I tried putting a # in my top-level paths, but it seems to be trying to do me a favor and url-protects the # by turning it into %23, which defeats the purpose.

Am I missing something?

答案1

得分: 1

在核心部分,React Navigation不支持包含哈希的URL以进行通用链接。

假设您已经设置并配置了您的应用程序以进行通用链接。

检测应用程序是否通过通用链接(https://example.com/#/path/to/route)打开,然后手动导航到对应于该URL的特定屏幕。

useEffect(() => {
  const deepLinkToScreen = async () => {
    // 获取用于打开应用程序的深链接
    const initialUrl = await Linking.getInitialURL();

    if (initialUrl !== null) {
      // 应用程序通过通用/深链接打开。
      // 解析initialUrl并提取参数等信息
      

      navigation.navigate("[SPECIFIC_SCREEN_NAME_FOR_THIS_URL]");
    }
  };

  deepLinkToScreen();
}, []);
英文:

At Core, React Navigation doesn't support URLs including hash for universal linking.

Assuming that you have set up and configured your app for universal linking.

Detect when the app opened with a universal link (https://example.com/#/path/to/route) then manually navigate to a specific screen corresponding to that URL.

useEffect(() => {
  const deepLinkToScreen = async () => {
    // Get the deep link used to open the app
    const initialUrl = await Linking.getInitialURL();

    if (initialUrl !== null) {
      // The app opened via universal/deep link. 
      // Parse initialUrl and extract parameters and more
      

      navigation.navigate("[SPECIFIC_SCREEN_NAME_FOR_THIS_URL]");
    }
  };

  deepLinkToScreen();
}, []);

huangapple
  • 本文由 发表于 2023年7月3日 04:43:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定