0

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:

  • https://localhost:3000 renders 1
  • https://localhost:3000/home renders 2
  • https://localhost:3000/user renders 3
  • https://localhost:3000/other renders 4

The following paths do not do what I expect:

  • https://localhost:3000/user/profile renders a blank page rather than 3.1
  • https://localhost:3000/user/account renders a blank page rather than 3.2
  • https://localhost:3000/user/other renders a blank page rather than 4
  • https://localhost:3000/other/other renders a blank page rather than 4

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

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
yaz_ozzie
  • 33
  • 1
  • 5
  • Even the simplest possible route involving multiple segments (e.g. `content} />`) renders a blank page and shows the same console error when hitting `https://localhost:3000/home/test`... – yaz_ozzie Apr 27 '22 at 15:50
  • What route are you typing in browser, because on the parent route component, you have typed `3}>`. In the following paths you've mentioned you've typed `http://localhost:3000/users/profile` it should be `http://localhost:3000/user/profile` – Inder Apr 27 '22 at 15:51
  • Are you typing `http://localhost:3000/user/profile` in browser? user singular not users – Inder Apr 27 '22 at 15:52
  • Sorry typo in my question - I am typing `user` – yaz_ozzie Apr 27 '22 at 16:10
  • I'm not getting a blank page, instead on accessing "user/profile" I'm getting the element defined on "user" route itself. – Inder Apr 27 '22 at 16:19
  • Which I guess is because there is no ``? – yaz_ozzie Apr 27 '22 at 16:25
  • Thinking about this, I am using IIS and URL Rewrite behind the scenes - I wonder if this could be causing issues? This issue seems similar: https://stackoverflow.com/questions/39967921/how-do-i-setup-react-router-clean-urls-with-iis. - I will try this and see if it fixes the issue... – yaz_ozzie Apr 27 '22 at 16:26
  • So I do get to see the user/profile-Component when I include an Outlet like so:
    3
    >}>
    – Andreas.Ludwig Apr 27 '22 at 16:29

2 Answers2

1

Check this working example in Codesandbox

Inder
  • 1,711
  • 1
  • 3
  • 9
0

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.

yaz_ozzie
  • 33
  • 1
  • 5
  • Update on this - actually that fix was unnecessary. What _really_ solved the problem was adding: `module.exports.publicPath = "/"` to my webpack config. – yaz_ozzie Apr 28 '22 at 09:48