how to get special characters in URL parameters by searchParams.get()
sample url:
https://xyz.herokuapp.com/project-invitations/role?projectInvitationId=000&projectName=TestProject&token=abc123&inviteeEmail=cvkude+1@gmail.com
focus on inviteeEmail parameter which has a value equal to cvkude+1@gmail.com
let’s assume we need to get the inviteeEmail parameter from the URL, we can do so by using searchParams.get(“inviteeEmail”)
- where using searchParams.get() is which is derived from `react-router-dom`
const inviteeEmail = searchParams.get(“inviteeEmail”);
in the above line of code if we console.log(inviteeEmail) then you would see output to be equal to cvkude 1@gmail.com and not cvkude+1@gmail.com
do you notice something unusual? The + symbol was replaced with the empty space “ ”.
the reason is, you need to encode the query parameters before combining them to form a url.
so if you pass the encoded parameter inviteeEmail=cvkude%2B1%40gmail.com then the below code would return the correct output viz. cvkude+1@gmail.com
const inviteeEmail = searchParams.get(“inviteeEmail”);
console.log(inviteeEmail) // cvkude+1@gmail.com
if you liked this article or found it useful, a follow would be much appreciated. Alternatively, you could buy me a coffee! All the support is much appreciated. :-)