-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useRouteLoaderData in the main Layout component (root.tsx) results in Invalid hook call, TypeError: Cannot read properties of null (reading 'useContext') #12786
Comments
im having the same issue in a different context, if my app is idle for some time, and i navigate im also getting I think that it related to the useRouteLoaderData loosing context for some reason |
I'll be following along here, since we are experiencing the same issue |
This only happens locally and on first page/route load here. |
@DennisKraaijeveld same to me, since I wrapped my layout in a NuqsAdapter |
This also appears to happen with |
I've left a comment on the remix repo about a similar issue. Could this also describe your issue? |
For anyone who encounters this in their development, please read this discord message by the @tomerzcod7 It solved my issue on dev. |
Hey guys, I found a solution that fixed this for me (for the most part), this is a copy paste from the Remix discord For anyone fighting with the "Cannot read properties of null (reading 'useContext')" error on dev server startup, when installing new dependencies or deleting node_modules etc, I've gone deep down the rabbit hole with this one and I found 2 solutions that fix this issue for me. Seems like this is caused by the way Vite dev server does dependency optimization, which happens when there's no vite cache (node_modules/.vite) folder, or when the cache is expired, when you install new dependency, package-lock changes, etc. I'm still not sure exactly which dependencies cause this and why but some dependencies when they are being optimized on initial page load cause the client app to crash. One fix that worked for me was using the Vite server warmup option (https://vite.dev/config/server-options.html#server-warmup) like this:
I basically needed to keep adding files here until at some point the app stopped crashing (even though it still optimizes some dependencies, apparently specific dependencies are causing the issue). Another solution I found which is a bit easier is using a future remix flag - https://remix.run/docs/en/main/guides/dependency-optimization
After setting this up, Vite doesn't need to optimize any dependencies on initial page load except of the react-router-devtools/client package, which still causes my app to crash. To fix the issue with that one I had 2 choices either remove the devtools completely from vite.config file or do this:
If anyone knows about any downsides to these fixes (especially about the unstable_optimizeDeps flag) would love to get some more info 🙂 Hope this helps! |
I'm using React Router as a...
framework
Reproduction
Every once in a while, when I start my RR7 app, I get an error from the main Layout component in the root.tsx file. It doesn't happen every time and usually resolves after I refresh the page.
This my very basic root.tsx file, with just added support color scheme type hint:
It seems like the error originates from calling the useRouteLoaderData in the Layout component.
I've implemented this code according to https://reactrouter.com/explanation/special-files#layout-export docs
I appreciate any help and would be happy to share more information.
System Info
Used Package Manager
pnpm
Expected Behavior
The app should render properly without invalid hooks and and react context errors
Actual Behavior
I get the following errors in the browser:
The text was updated successfully, but these errors were encountered: