-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Description
I'm using React Router as a...
framework
Reproduction
Repository
https://github.com/Vinorcola/React-router-global-layout-remount-issue
Install
Clone and run npm install in the directory.
Then, run npm run dev to start the server and open http://localhost:5173
Understand
There is a GlobalComponent (in app folder) that is mounted in the root layout. This component has it's own state that should only be initialized once during the whole app life. It is displayed at the bottom of the screen.
There is a menu with several links:
- Home: the home page
- Other: another page
- Loader eroor: a page where an error is thrown from the loader
- Unknown: an unexisting page
What's wanted
The ability to have a global component that is never unmounted, even on error.
Issue
When navigating from successfull pages to any page with error (or vice versa), the global layout is unmounted and remonted
when switching between the rendering of the global ErrorBoundary and the global App components.
Hence, there is no way to have a global component that is never unmounted, even if an error occured.
Reproduce
- Open http://localhost:5173. Check the value displayed at the bottom of the screen.
- Open dev console (There is an Hydration error that can be ignored, it is due to randomness of the
GlobalComponent's state.) - Navigate from Home to Other and vice-versa. The content of the
GlobalComponentdoes not change. - Navigate to either Loader error or Unknown and you'llfind that the content of
GlobalComponenthas changed since it as been unmounted and remounted (check console for proof). - From ther, navigating to Loader error & Unknown pages does not affect the state.
- Navigate then to Home or Other, the state changes again.
System Info
System:
OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Memory: 13.53 GB / 15.57 GB
Container: Yes
Shell: 5.1.16 - /bin/bash
Binaries:
Node: 22.14.0 - /usr/local/bin/node
npm: 10.9.2 - /usr/local/bin/npm
npmPackages:
@react-router/dev: ^7.4.0 => 7.4.0
@react-router/node: ^7.4.0 => 7.4.0
@react-router/serve: ^7.4.0 => 7.4.0
react-router: ^7.4.0 => 7.4.0
vite: ^5.4.11 => 5.4.14Used Package Manager
npm
Expected Behavior
The root layout should not be unmounted when switching from successfull pages to error pages (or vide versa)
Actual Behavior
The root layout is unmounted and remounted when switching from successfull pages to error pages (or vide versa)