Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decode url before location change in react app

Hey guys can you help me solve next issue? For example we have url like site.com/post?comments=1,2,3,4 after I paste it in browser address string, my app opens and url decodes to site.com/post?comments=1%2C2%2C3%2C4, how to get rid of %2C in url and save it in original way and vice versa if someone opens url like site.com/post?comments=1%2C2%2C3%2C4 decode it to site.com/post?comments=1,2,3,4? I know that I can use method lice decodeURIComponent, but I don't know where and in which moment exactly apply it. I'm using react and react-router. Any ideas?

like image 898
David Avatar asked Dec 14 '25 01:12

David


1 Answers

You can use decodeURIComponent for decoding and encodeURIComponent and encoding. To see the result run the snippet below:

console.log(decodeURIComponent(`site.com/post?comments=1%2C2%2C3%2C4`));
console.log(encodeURIComponent(`site.com/post?comments=1,2,3,4`));
like image 112
Andrzej Smyk Avatar answered Dec 16 '25 18:12

Andrzej Smyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!