You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When changing a file (random-component.tsx), containing a circular reference to another component containing a context (value-provider.tsx), the context is set to its initial value.
I expect the context to never contain the initial value, but always contain the value I designed it to have (see the provider, which always wraps this component), as I have designed my app around this criteria, which always will yield true in production, but seems broken in development. I guess this is due to hot-module-reloading?
It looks like this is due to HMR. "Easier-to-say" suggestion is to avoid cyclic import, but this might be technically a bug.
When modifying random-component.tsx, it will (soft) invalidate importers and client will run new module random-component.tsx?t=xxx, but due to cyclic import it will also run value-provider.tsx?t=xxx but not show-value.tsx?t=xxx, thus ShowValue is accessing an old context, which is not rendered anymore (I think).
Probably more well-defined behavior would be to have full-reload at the point value-provider.tsx?t=xxx is run during HMR, but current behavior might be saving some other cyclic imports scenario where HMR can work in general, so this choice might be trade-off.
If you fix one or the other it should work. I've created a lint rule proveded in the default template that can help with following consistent exports. For React context, this requires having two files: one for context + hook, and one for the provider.
Also for top level singleton context for which initial value is known ahead of time, zustand is a good replacement of React context for me with less boilerplate
Uh oh!
There was an error while loading. Please reload this page.
Related plugins
plugin-react
plugin-react-swc
plugin-react-oxc
Describe the bug
When changing a file (
random-component.tsx
), containing a circular reference to another component containing a context (value-provider.tsx
), the context is set to its initial value.This creates a problem in my scenario, because the context should never contain the initial values provided, but can (in a normal react life-cycle) only contain the data set in the
ValueProvider
component. This is known as the provider-pattern (https://medium.com/@vitorbritto/react-design-patterns-provider-pattern-b273ba665158).I expect the context to never contain the initial value, but always contain the value I designed it to have (see the provider, which always wraps this component), as I have designed my app around this criteria, which always will yield true in production, but seems broken in development. I guess this is due to hot-module-reloading?
I've seen this problem both in react 19 and 18.
Reproduction
https://stackblitz.com/edit/vitejs-vite-22uuzwxi
Steps to reproduce
npm install && npm run dev
http://localhost:5173/
)"Test"2
on the screen.src/page-layout.tsx
in your favourite editor and replace the2
in line 9 by3
.3
, but I would expect to see"Test"3
.System Info
Used Package Manager
pnpm
Logs
The logs before changing the file:
The logs after changing the file:
Validations
The text was updated successfully, but these errors were encountered: