React Navigation 动态屏幕和深度链接

发布于 2025-01-14 14:40:22 字数 315 浏览 2 评论 0原文

我想通过反应导航创建动态选项卡屏幕,但我不知道如何通过深层链接来解决它。

我希望我的选项卡屏幕可以通过深度链接访问,例如: /tabs/:tabId 但我不知道如何处理链接配置。 如果有人可以帮助我,我已经创建了这个小吃:

https://snack.expo.dev/@natinho68/getting-started-%7C-react-navigation

I want to create dynamically tab screens with react navigation, but I don't know how to figure it out with the deep linking.

I would like to have my tabs screens accessible with deeplinking like : /tabs/:tabId but I don't know how to deal with the linking config.
If there is someone who can help me, I have created this snack :

https://snack.expo.dev/@natinho68/getting-started-%7C-react-navigation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮世清欢 2025-01-21 14:40:22

首先,您需要通过文档
进行设置

这是一个最小的示例。 Root stack 是一个堆栈导航器,有 3 个屏幕:启动画面、登录和应用程序。 App 是另一个堆栈导航器(嵌套),有 2 个屏幕:仪表板和登录。

const deepLinkDevPrefix = "https://custom.server.org"
const deepLinkLocalProtocol = "customprotocol://"
const deepLinkConfig = {
  screens: {
    Splash: "Splash",
    Login: "Login",
    App: {
      path: "App",
      screens: {
        Dashboard: "Dashboard",
        Profile: "Profile,
      },
    },
  }
}

在 getInitialURL 内部,您可以处理打开应用程序的 url。

const App = () => {
  const getInitialURL = async () => {
    // Check if app was opened from a deep link
    const url = await Linking.getInitialURL();

    console.log('This is url')
    console.log(url)

    if (url !== null) {
      return url;
    }

    return undefined
  }
  
  const linking = {
    prefixes: [deepLinkDevPrefix, deepLinkLocalProtocol],
    deepLinkConfig,
    getInitialURL
  };

  return (
    <NavigationContainer linking={linking}>
      <RootStack />
    </NavigationContainer>
  );
}

在这里您可以了解如何在没有导航属性的情况下进行导航。

First you need to set it up via documentation

This is minimal example. Root stack is a stack navigator which has 3 screens: Splash, Login and App. And App is another stack navigator(nested) with 2 screens: Dashboard and Login.

const deepLinkDevPrefix = "https://custom.server.org"
const deepLinkLocalProtocol = "customprotocol://"
const deepLinkConfig = {
  screens: {
    Splash: "Splash",
    Login: "Login",
    App: {
      path: "App",
      screens: {
        Dashboard: "Dashboard",
        Profile: "Profile,
      },
    },
  }
}

Inside of getInitialURL you can handle url that opened the app.

const App = () => {
  const getInitialURL = async () => {
    // Check if app was opened from a deep link
    const url = await Linking.getInitialURL();

    console.log('This is url')
    console.log(url)

    if (url !== null) {
      return url;
    }

    return undefined
  }
  
  const linking = {
    prefixes: [deepLinkDevPrefix, deepLinkLocalProtocol],
    deepLinkConfig,
    getInitialURL
  };

  return (
    <NavigationContainer linking={linking}>
      <RootStack />
    </NavigationContainer>
  );
}

Here you can find out how to navigate without navigation prop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文