"[vite] hot updated" fires but React component state resets on every edit — Fast Refresh boundary issue?
0 reputation · 17 May 2023, 15:48 UTC
0 reputation · 17 May 2023, 15:48 UTC
I'm seeing a consistent behavior in a Vite dev server setup where HMR appears to work — the console logs the hot update event and no full page reload occurs — yet the edited React component loses all of its local state on every save. My understanding is that Fast Refresh should preserve component state across edits when the module is structured correctly.
From the documentation, Fast Refresh relies on a file exporting only React components (or components plus hooks) for state preservation to work. My component file also exports a couple of plain utility constants and a non-component helper function, and I suspect this mixed export pattern is what forces a fallback that remounts the component instead of refreshing it.
Relevant constraints: this is on Vite 5.x with the official React plugin, and I know HMR behavior around CSS and side-effectful modules changed between major versions, so I want to be sure I'm reasoning about the current semantics. The module does not call import.meta.hot.accept() manually.
My questions:
import.meta.hot.accept() handler change this behavior, or does the React plugin override it?32275 reputation · 17 May 2023, 23:05 UTC
[Fast Refresh] that appears when a module is accepted via Fast Refresh; its absence indicates a fallback remount.import.meta.hot.accept() handler does not override the plugin’s internal logic; the React plugin replaces or ignores custom handlers, so it will not change the fallback behavior.Your component file exports a React component together with plain constants and a helper function. Because the module contains non‑component exports, the plugin treats the whole module as ineligible for Fast Refresh, falls back to a remount, and re‑initializes all useState, useReducer, and useRef values on each save.
[Fast Refresh]. If you do not see it, the module is being remounted.utils.js) and import them where needed.[Fast Refresh] message and the component’s local state should persist.If you are not seeing the [Fast Refresh] message after making the change, let me know whether any other HMR‑related logs appear; that detail would affect whether we need to investigate plugin version or side‑effectful imports.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.