Any time I create a route with more than one "/"
(i.e. "/testing/test"
) react just returns a blank page when it should return the Test
component as well as the navigation bar and footer. In the example below the route "/testing"
works as expected but "/testing/test"
does not.
Here is my router:
<UserProvider>
<BrowserRouter>
<Navbar />
<Routes>
<Route path="/">
<Route index element={<Grid />} />
<Route path="testing">
<Route index element={<Test />} />
<Route path="/test" element={<Test />} />
</Route>
<Route path="*" element={<Error />} />
</Route>
</Routes>
<Footer
legal={threat_intel.bottom_disclaimer}
lastUpdate={threat_intel["Data Last Update"]}
/>
</BrowserRouter>
<ToastContainer />
</UserProvider >
- React – v18.2.0
- react-router-dom – v6.16.0
2
Answers
This ended up being a completely separate issue.Wrapped around my router I had an if statement that was returning false when I assumed it was returning true. I probably should have included that in my code block. But I was fetching a .json file with data in it and was using a relative path so when my url was "/testing/test" it was looking for the file under "/testing/{filename.json}"
You are rendering a nested route with an invalid absolute path. It produces the following error:
Either fully quantify the route path, e.g.
<Route path="/testing/test" element={<Test />} />
or use a relative path, e.g.<Route path="test" element={<Test />} />
.Using relative paths is easier to read when nesting routes.