I am new to React and trying to render a page based on the name query like http://localhost:3000/show?name=james
So I initially added the Route as :
<BrowserRouter>
<Switch>
<Route path='*' component={ErrorComponent} />} />
<Route path="show(/:name)" name="name" component={Show}></Route>
</Switch>
</BrowserRouter>
And then I tried to render the component Show like below:
import React, { Component } from 'react';
import queryString from 'query-string';
class Show extends Component {
componentDidMount(){
console.log(this.props.location.search);
const values = queryString.parse(this.props.location.search);
console.log(values.name);
}
render() {
const { params } = this.props.match;
return <div>
<h4>About</h4>
<p>This is About page.</p>
{params.id ? <b>ID: {params.id}</b> : <i>ID is optional.</i>}
</div>
}
}
export default Show;
then when I try to show the page
http://localhost:3000/show?name=james
It always show 404. Not sure which part I am doing it wrong. Any help is appreciated. Also I am using react-router-dom 5.1.2 . Thanks.