|
| 1 | +--- |
| 2 | +"react-router": patch |
| 3 | +"react-router-dom": patch |
| 4 | +"@remix-run/router": patch |
| 5 | +--- |
| 6 | + |
| 7 | +fix: remove internal router singleton (#9227) |
| 8 | + |
| 9 | +This change removes the internal module-level `routerSingleton` we create and maintain inside our data routers since it was causing a number of headaches for non-simple use cases: |
| 10 | + |
| 11 | +- Unit tests are a pain because you need to find a way to reset the singleton in-between tests |
| 12 | + - Use use a `_resetModuleScope` singleton for our tests |
| 13 | + - ...but this isn't exposed to users who may want to do their own tests around our router |
| 14 | +- The JSX children `<Route>` objects cause non-intuitive behavior based on idiomatic react expectations |
| 15 | + - Conditional runtime `<Route>`'s won't get picked up |
| 16 | + - Adding new `<Route>`'s during local dev won't get picked up during HMR |
| 17 | + - Using external state in your elements doesn't work as one might expect (see #9225) |
| 18 | + |
| 19 | +Instead, we are going to lift the singleton out into user-land, so that they create the router singleton and manage it outside the react tree - which is what react 18 is encouraging with `useSyncExternalStore` anyways! This also means that since users create the router - there's no longer any difference in the rendering aspect for memory/browser/hash routers (which only impacts router/history creation) - so we can get rid of those and trim to a simple `RouterProvider` |
| 20 | + |
| 21 | +```jsx |
| 22 | +// Before |
| 23 | +function App() { |
| 24 | + <DataBrowserRouter> |
| 25 | + <Route path="/" element={<Layout />}> |
| 26 | + <Route index element={<Home />}> |
| 27 | + </Route> |
| 28 | + <DataBrowserRouter> |
| 29 | +} |
| 30 | + |
| 31 | +// After |
| 32 | +let router = createBrowserRouter([{ |
| 33 | + path: "/", |
| 34 | + element: <Layout />, |
| 35 | + children: [{ |
| 36 | + index: true, |
| 37 | + element: <Home />, |
| 38 | + }] |
| 39 | +}]); |
| 40 | + |
| 41 | +function App() { |
| 42 | + return <RouterProvider router={router} /> |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +If folks still prefer the JSX notation, they can leverage `createRoutesFromElements` (aliased from `createRoutesFromChildren` since they are not "children" in this usage): |
| 47 | + |
| 48 | +```jsx |
| 49 | +let routes = createRoutesFromElements( |
| 50 | + <Route path="/" element={<Layout />}> |
| 51 | + <Route index element={<Home />}> |
| 52 | + </Route> |
| 53 | +); |
| 54 | +let router = createBrowserRouter(routes); |
| 55 | + |
| 56 | +function App() { |
| 57 | + return <RouterProvider router={router} /> |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +And now they can also hook into HMR correctly for router disposal: |
| 62 | + |
| 63 | +``` |
| 64 | +if (import.meta.hot) { |
| 65 | + import.meta.hot.dispose(() => router.dispose()); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +And finally since `<RouterProvider>` accepts a router, it makes unit testing easer since you can create a fresh router with each test. |
| 70 | + |
| 71 | +**Removed APIs** |
| 72 | + |
| 73 | +- `<DataMemoryRouter>` |
| 74 | +- `<DataBrowserRouter>` |
| 75 | +- `<DataHashRouter>` |
| 76 | +- `<DataRouterProvider>` |
| 77 | +- `<DataRouter>` |
| 78 | + |
| 79 | +**Modified APIs** |
| 80 | + |
| 81 | +- `createMemoryRouter`/`createBrowserRouter`/`createHashRouter` used to live in `@remix-run/router` to prevent devs from needing to create their own `history`. These are now moved to `react-router`/`react-router-dom` and handle the `RouteObject -> AgnosticRouteObject` conversion. |
| 82 | + |
| 83 | +**Added APIs** |
| 84 | + |
| 85 | +- `<RouterProvider>` |
| 86 | +- `createRoutesFromElements` (alias of `createRoutesFromChildren`) |
0 commit comments