0

I have a react app, and I'm using react-router, I have a few nested routers, that seem to work fine.

However, when I refresh the page, even though the URL of my browser is something like /admin/users/blah/ nothing is loaded by react-router, it's as if none of the <Route> components get triggered when the page is refreshed by the user. I see my site's navigation menu, but the content that is supposed to be rendered with a router is just blank, again, as if no <Route> matched the current URL.

Are there any special considerations or boilerplate I need to add in order to have the expected behavior?

I would expect react-router to match the <Route> components again, and render according to my browser's URL, but it doesn't do that.

Blaine Lafreniere
  • 3,451
  • 6
  • 33
  • 55
  • did you set up your backend for a a single page app (SPA)? e.g. https://stackoverflow.com/q/44098136/1176601 is about nginx => serve the `index.html` for all unknown routes at least, instead of HTTP 404, unless you want some server side rendering (SSR) – Aprillion May 18 '20 at 13:03
  • This is on my development machine. React is being served via `npm run start` on port 3000, and the backend is served by rails which is running on my local machine in development mode on port 3001, I believe it's just using Puma for the dev server. – Blaine Lafreniere May 18 '20 at 13:06
  • don't know anything about Puma, only that [create react app](https://reactjs.org/docs/create-a-new-react-app.html) comes with preconfigured webpack-dev-server that works as expected for client-side-only routing on localhost – Aprillion May 18 '20 at 13:16
  • So under normal circumstances, react-router should be able to re-render the page I was on previously, before I had initiated a browser refresh? – Blaine Lafreniere May 18 '20 at 13:30
  • ehm, no idea what you consider "normal" – Aprillion May 18 '20 at 17:21

1 Answers1

0

I figured out my issue, I had multiple nested <Router> elements. You only actually need one. You can nest as many <Switch> elements as you want, though.

Blaine Lafreniere
  • 3,451
  • 6
  • 33
  • 55