kkesley
kkesley

Reputation: 3406

React routes param auto decode string

Hi does react router params automatically decodes string?

For example i have this route

<Route path="/callback/:url"/>

and when I call it with C5jb20%3D

it will print C5jb20= when I console.log() that parameters

Is this an expected behavior? Can I change this behavior so that I still get the encoded string?

Thanks!

Upvotes: 6

Views: 9197

Answers (1)

Andy Ray
Andy Ray

Reputation: 32076

C5jb20%3D is a URL encoded string, which is a way to encode special characters in a URL. For example, if you have a URL parameter like ?redirect=/some/path, a server could interpret the forward slashes as routing paths, and mess up the expected routing. That's why you see things like ?redirect=%2Fsome%2Fpath, and your example, in URLs.

window.location.path will have the original, unencoded URL.

You could also get it back with encodeURIComponent:

encodeURIComponent('C5jb20=') // "C5jb20%3D"

You probably don't want to, though, depending on your use case.

Upvotes: 4

Related Questions