React Navigation v5:如何与Branch.io一起使用

时间:2020-10-26 22:37:14

标签: react-native react-navigation react-navigation-v5

我们有一个利用Branch.io通用链接的应用程序。遵循以下文档: https://help.branch.io/developers-hub/docs/react-native#read-deep-link

在react-native应用程序上,您设置了一个订阅者,以在javascript运行时中接收深层通用链接。

从此处使用最新的react-navigationhttps://reactnavigation.org/docs/deep-linking

React Navigation希望原生处理深层链接。 React Navigation似乎没有提供手动启动链接的好方法。

我如何一起使用这两项服务?事实证明,将深层链接分解为可路由的深层链接具有挑战性。我们的应用程序嵌套了路由器,并且我不希望重做从路径到屏幕和参数的转换。最近有人做过吗?谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用subscribe选项将传入链接通知React Navigation:

const linking = {
  // Custom susbcription to handle incoming links
  subscribe(listener) {
    branch.subscribe(({ error, params, uri }) => {
      if (error) {
        console.error('Error from Branch: ' + error);
        return;
      }

      if (params['+non_branch_link']) {
        const nonBranchUrl = params['+non_branch_link'];
        // Route non-Branch URL if appropriate.
        return;
      }

      if (!params['+clicked_branch_link']) {
        // Indicates initialization success and some other conditions.
        // No link was opened.
        return;
      }

      // A Branch link was opened
      const url = params.$canonical_url;

      listener(url);
    });

    return () => {
      // Clean up the event listeners
      branch.unsubscribe();
    };
  },
  // Options such as prefixes, screens config etc.
  // ...
};

文档:https://reactnavigation.org/docs/deep-linking/#third-party-integrations