【问题标题】:Is there a way to navigate to a default screen (Profile) if the path name does not match a route that I define explicitly?如果路径名与我明确定义的路由不匹配,有没有办法导航到默认屏幕(配置文件)?
【发布时间】:2020-08-23 17:22:59
【问题描述】:

我已尝试根据react-navigation configuring links docs 为我的 react 本机应用程序编写配置对象。

我定义了一系列路径 --> 路由映射包括 Chat:'feed' 和 Explore:'news'

我的目标是让应用程序在解析https://example.com/someusername 时默认进入配置文件屏幕,并将“someusername”作为 id 参数传递。类似于https://instagram.com/apple 直接映射到@apple 帐户的配置文件屏幕。

const linking = {
  prefixes: ['https://example.com', 'example://'],
  config: {
    Home: {
      path: '',
      screens: {
        Chat: 'feed',
        Explore: 'news',
        Profile: ':id',
      },
    },
  },
}

我不允许用户创建@feed、@news 等帐户以避免冲突。如果“someusername”尚不存在,我也会显示“未找到用户”消息

有没有正确的方法来做这个配置对象?或者如果我需要编写自定义的 getStateFromPath / getPathFromState 函数,有人有例子吗?

【问题讨论】:

    标签: android ios react-native react-navigation deep-linking


    【解决方案1】:

    我们现在没有内置的方法来做这件事,但是你可以提供一个自定义的getStateFromPath 函数来返回一个不同的状态对象:

    const linking = {
      prefixes: ['https://mychat.com', 'mychat://'],
      config: {
        Chat: 'feed/:sort',
      },
      getStateFromPath(path, config) {
        // You can perform own checks on the path
        if (isValidPath(path)) {
          return {
            routes: [
              {
                name: 'Home',
                state: {
                  routes: [{ name: 'Profile' }],
                },
              },
            ],
          };
        }
    
        return getStateFromPath(path, config);
      },
    };
    

    https://reactnavigation.org/docs/use-linking#getstatefrompath

    关于未找到用户消息,我建议在Profile 屏幕本身中通过检查参数并在那里呈现错误消息来处理该消息。

    【讨论】:

      猜你喜欢
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 2016-04-29
      相关资源
      最近更新 更多