how to set query params in react code example
Example 1: react parameter value from query string
import queryString from 'query-string'
const value=queryString.parse(this.props.location.search);
const token=value.token;
console.log('token',token)
const query = new URLSearchParams(this.props.location.search);
const token = query.get('token')
console.log(token)
const getQueryParams = () => window.location.search.replace('?', '').split('&').reduce((r,e) => (r[e.split('=')[0]] = decodeURIComponent(e.split('=')[1]), r), {});
Example 2: append a query string to the url react
history.push({
pathname: '/dresses',
search: '?color=blue'
})
Example 3: use query params react
const query = new URLSearchParams(this.props.location.search);
const token = query.get('token')
console.log(token)
Example 4: get query params react
new URLSearchParams(this.props.location.search).get("your_query_param_key")
Example 5: history push search params
this.props.history.push({
pathname: '/client',
search: "?" + new URLSearchParams({clientId: clientId}).toString()
})