Answer the question
In order to leave comments, you need to log in
Why nested routes don't work in react-router-dom?
It is necessary that the /products
component opens along the way Products
. It works.
But /products/123
it should display the component Product
, which would have available in props what comes after products
, i.e. 123. This does not work.
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<BrowserRouter>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/products">Products</Link>
</li>
<li>
<Link to="/products/1">Product 1</Link>
</li>
</ul>
<Switch>
<Route path="/" component={Home} exact />
<Route path="/products" component={Products} />
<Route path="/products/:slug" component={Product} />
</Switch>
</BrowserRouter>
</div>
);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question