You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用React-Navigation深链接时如何将参数封装为对象传递

你可以使用React Navigation深链接配置中的transform配置项实现需求,该配置会在所有参数经过parse解析完成后执行,可自定义修改最终输出到route.params的结构。

修改后的完整配置代码:

prefixes: [Linking.makeUrl('/'), 'https://test.app', 'https://*.test.app'],
config: {
  screens: {
    App: {
      screens: {
        Chat: {
          path: 'chat/:firstName/:lastName/:birthdate',
          parse: {
            firstName: (firstName: string) => decodeURIComponent(firstName),
            lastName: (lastName: string) => decodeURIComponent(lastName),
            birthdate: (birthdate: string) => decodeURIComponent(birthdate),
          },
          // 新增transform配置组装userObject
          transform: (params: Record<string, any>) => {
            const { firstName, lastName, birthdate, ...otherParams } = params
            return {
              ...otherParams,
              userObject: {
                firstName,
                lastName,
                birthdate
              }
            }
          }
        }
      }
    }
  }
}

注意事项:

  • transform配置仅在React Navigation 6.x及以上版本支持,若使用更低版本,可在页面拿到route.params后手动组装userObject对象使用。
  • 配置完成后可直接在Chat页面通过const { userObject } = route.params获取封装后的用户参数。

内容的提问来源于stack exchange,提问作者KarimK3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 10:39:00