The problem of passing parameters with special characters in react-router

when the passed parameter contains? (question mark), the value obtained by using this.props.match.params is not desired, because at this time? (question mark) is regarded as the query string (querystring), how to deal with this situation?

/:id  id"12?34",this.props.match.params.id12"12?34"
Mar.23,2021

encodeURIComponent
then go to decodeURIComponent


you can try a very short and useful library: query-string .

const queryString = require('query-string');
 
console.log(location.search);
//=> '?foo=bar'
 
const parsed = queryString.parse(location.search);
console.log(parsed);
//=> {foo: 'bar'}
 
console.log(location.hash);
//=> '-sharptoken=bada55cafe'
 
const parsedHash = queryString.parse(location.hash);
console.log(parsedHash);
//=> {token: 'bada55cafe'}
 
parsed.foo = 'unicorn';
parsed.ilike = 'pizza';
 
const stringified = queryString.stringify(parsed);
//=> 'foo=unicorn&ilike=pizza'
 
location.search = stringified;
// note that `location.search` automatically prepends a question mark
console.log(location.search);
//=> '?foo=unicorn&ilike=pizza'
Menu