I m trying to make multiple react pages but react doesn’t show any of the new pages . I think my code is correct , I am using npm install react-router-dom@6
and this is my code :
Index.js
JavaScript
x
12
12
1
ReactDOM.render(
2
<BrowserRouter>
3
<Routes>
4
<Route path="/" element={<App />}>
5
<Route path="pricing" element={<Pricing />} />
6
<Route path="contact" element={<Contact />} />
7
</Route>
8
</Routes>
9
</BrowserRouter>,
10
document.getElementById('root')
11
);
12
in the Navbar.jsx :
JavaScript
1
3
1
<Link to="/pricing">Pricing</Link>
2
<Link to="/contact">Contact</Link>
3
and for example Pricing.js :
JavaScript
1
4
1
<div className='Pricing'>
2
<Navbar />
3
</div>
4
Any idea how to fix this ? where I have missed it exactly ?
Advertisement
Answer
First you are missing /
on some path. Second, the first Route will catch all of them, because all of them start with /
. To avoid that, you need the keyword exact
, like so:
JavaScript
1
11
11
1
ReactDOM.render(
2
<BrowserRouter>
3
<Routes>
4
<Route exact path="/" element={<App />} />
5
<Route exact path="/pricing" element={<Pricing />} />
6
<Route exact path="/contact" element={<Contact />} />
7
</Routes>
8
</BrowserRouter>,
9
document.getElementById('root')
10
);
11