I am trying to apply the answer given in this post How to parse a query string in React Router
but it's not working.
This is my url:
`/api/lexemes?searchTerm=${searchTerm}&optionValue=${optionValue}`
First, nothing shows when I run this code:
let search = window.location.search;
let params = new URLSearchParams(search);
let foo = params.get('searchTerm');
console.log(foo);
nor does this one:
let url = this.props.location.search;
let params = queryString.parse(url);
console.log(params);
When I do:
console.log(this.props);
The console shows Location with search= ""
Please note that I am using Class Components, does it work only with functional ones? If you could tell me how to do this with a class component that would be great.
My paths in App.js look like this now:
<Switch>
<Route exact path="/" component={Main} />
...
</Switch>
What I would like to have is:
<Route exact path="/:search/:option" component={Main} />
This is my axios call in Main.js
handleSubmit = (searchTerm, optionValue) => {
axios
.get(`/api/lexemes?searchTerm=${searchTerm}&optionValue=${optionValue}`, {
})
.then(
(res) => {
if (Array.isArray(res.data) && res.data.length === 0) {
this.setState({
noData: 1,
});
} else {
this.setState({
words: res.data,
currentScreen: "WordItem",
});
}
},
(error) => {
alert("handleSubmit error " + error);
}
);
};
withRouterHOC. You can then access the query string in your location prop