I have the following setup:
<div>
<Router history={browserHistory}>
<Route path="/" component={NewCustomerContainer} />
<Route path="/newCustomer" component={NewCustomerContainer} />
<Route path="/search" component={SearchPageContainer} />
<Route path="/network" component={NetworkContainer} />
<Route path="/network/:id" component={NetworkContainer} ></Route>
</Router>
</div>
The route http://localhost:8100/network
works fine. The route http://localhost:8100/network/abc123
does not with a 404 error appearing in my console. Anyone seen anything like this before?
To get path params in React Router, we can use the useParams hook. We create the Child component that calls the useParams hook to return an object with the route params in the URL. And we render the value of the id param on the page. In App , we have 2 links that goes to routes with different id param values.
Getting the URL params In React router v4, you can access it using the props.match.params.id . In class-based components, you can access it like this. import React, { Component } from "react"; class Users extends Component { render() { const { id } = this.
Something else to try is to add a
<base href="/">
tag inside the head tags of your base HTML page.
<div>
<Router history={browserHistory}>
<Route path="/" component={NewCustomerContainer} />
<Route path="/newCustomer" component={NewCustomerContainer} />
<Route path="/search" component={SearchPageContainer} />
<Route path="/network" component={NetworkMetaContainer}>
<Route path="/:id" component={NetworkContainer}/>
</Route>
</Router>
</div>
You forgot to nest the the '/id' inside the network Route. React Router allows for nested routing by placing Routes inside other Routes... If you have all network/id stuff inside the network and the network renders its children then this will work correctly.
The new NetworkMetaContainer will need to be built that has a simple render children... or if you want you can perhaps have a <IndexRoute />
inside the network Route, but either way the NetworkMetaContainer thing must be just the outer wrapper (maybe it'll have the different tabs or links or possibilities inside the Network)?
try to add prop exact={true}
inside Route
component
<div>
<Router history={browserHistory}>
<Route path="/" component={NewCustomerContainer} />
<Route path="/newCustomer" component={NewCustomerContainer} />
<Route path="/search" component={SearchPageContainer} />
<Route path="/network" exact={true} component={NetworkContainer} />
<Route path="/network/:id" exact={true} component={NetworkContainer} ></Route>
</Router>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With