yaz_ozzie
yaz_ozzie

Reputation: 53

React router doesn't handle urls containing more than one segment

I am struggling to get react router v6 to work properly. Given the below:

<BrowserRouter>
  <Routes>
    <Route index element={<div>1</div>} />
    <Route path="home" element={<div>2</div>} />
    <Route path="user" element={<div>3</div>}>
      <Route path="profile" element={<div>3.1</div>} />
      <Route path="account" element={<div>3.2</div>} />
    </Route>
    <Route path="*" element={<div>4</div>} />
  </Routes>
</BrowserRouter>

The following paths do what I expect:

The following paths do not do what I expect:

I am aware that including <Outlet /> in the parent component (e.g. that rendered by /user) allows child components to render within it. However this only works with an index route. As soon as a second slash (e.g. /user/account) appears in the path, the request fails. Equally, the * default only renders if the unknown url contains a single /.

I must be doing something silly here since this example is essentially lifted from the instructions (https://reactrouter.com/docs/en/v6). The error the browser shows in the incorrect scenarios is (in this case the bundle.js is simply my index.html file instead of the app JS):

Console error

Upvotes: 1

Views: 835

Answers (2)

yaz_ozzie
yaz_ozzie

Reputation: 53

Thanks for the help! In the end I think my issue was tied up with using IIS and URL rewrite under the hood, which none of the examples (reasonably) consider.

I hadn't properly set up my web.config file (as per the answer here: https://stackoverflow.com/a/39968035/5181023) or index.html to handle extended urls and once I had, urls with multiple segments worked as expected.

Upvotes: 1

Inder
Inder

Reputation: 2078

Check this working example in Codesandbox

Upvotes: 1

Related Questions