react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

Path values not decoded properly from URL pathname

Open LinusU opened this issue 2 years ago • 1 comments

Current behavior

Consider an app with two screens, Home and Test, and with the following linking options:

{ config: { screens: { Test: { path: 'test/:id' } } } }

In the Home screen there is a button which calls the following code:

navigation.navigate('Test', { id: 'test=' })

This changes the location field of the browser to /test/test%3D and switches to the Test screen. Currently useRoute().params.id correctly returns "test=".

However, doing a refresh in the browser will change the value returned from useRoute().params.id to test%3D.

The problem seems to be that values aren't passed via decodeURIComponent when read from location.pathname, and thus the encoded value %3D shows up instead of the = character.

Expected behavior

I expected useRoute().params.id to return "test=" even after a refresh.

Reproduction

https://github.com/LinusU/react-navigation-percent-encoding-issue

Platform

  • [ ] Android
  • [ ] iOS
  • [X] Web
  • [ ] Windows
  • [ ] MacOS

Packages

  • [ ] @react-navigation/bottom-tabs
  • [ ] @react-navigation/drawer
  • [ ] @react-navigation/material-top-tabs
  • [ ] @react-navigation/stack
  • [X] @react-navigation/native-stack
  • [ ] react-native-tab-view

Environment

  • [x] I've removed the packages that I don't use
package version
@react-navigation/native 6.1.7
@react-navigation/native-stack 6.9.13
react-native-safe-area-context 4.6.3
react-native-screens 3.22.1
react-native 0.72.3
expo 49.0.5
node 18.12.1
npm or ~~yarn~~ 8.19.2

LinusU avatar Jul 27 '23 11:07 LinusU

I've worked around this by decoding the parameter myself

screenName: {
    path: 'screenName/:id',
    parse: {
      id: (id: string) => decodeURIComponent(id),
    },
},

mhyassin avatar Feb 20 '24 13:02 mhyassin