skip to content

In a component framework's hot swap, why does editing a file that exports both a component and a constant reset state that other edits keep?

level: seniorimportance: should knowfreq 12%

answer

  1. components registered by identity
  2. module accepts its own updates
  3. importers hold non-component values
  4. update handed on to importers
  5. one kind of export per file

basics

~20 s

A state-keeping swap works only when every export is a component the runtime can swap by identity; importers hold a constant directly, so the tooling hands the update on to them, and the tree they render can remount or the page reloads.

solid answer

~40 s

In development the framework's transform registers each component under a stable identity and makes the module accept its own updates. On save, the new module re-registers its components and the framework re-renders the **existing instances** with the new code, so their local state survives. That only works if every export is a component. Importers hold a constant or helper directly, and re-running the module would leave them with the old value, so the tooling **hands the update on** (some only when the exported value changed). The update climbs to the importers: a component module there re-runs and the tree it renders can lose state, and if the walk reaches the entry, the page reloads. The fix is to move the constant into its own module.

go deeper

for a junior

Recall that component hot swaps keep local state only for some edits, and that a file exporting non-component values often loses it.

for a middle

Explain how components are registered and re-rendered in place, and why a non-component export makes the update climb to importers.

for a senior

Diagnose a state reset from the file's exports and the import graph, and restructure modules so that everyday edits stay hot swaps.

for a principal

Set the module conventions, such as one kind of export per file, that keep a large team's hot swaps reliable without slowing anyone down.

## How a state-keeping component swap works Component frameworks layer a smarter swap on top of **hot module replacement (HMR)**. In development, the framework's transform adds code to each component module that does two things: 1. It **registers** every component the module defines with the framework's refresh runtime, under a stable identity typically derived from the file and the component's name. 2. It makes the module **accept its own updates**, so the module is an accept boundary. When the file changes, the new version of the module runs, registers its components again, and the runtime tells the framework to re-render every **existing instance** of those components with the new code. Because the instances are not destroyed, their **local state** (the half-filled filter form, an expanded panel) carries over. Only the rendering logic changed. ## Limit 1: a module that exports more than components The trick depends on the module being safe to accept itself. That holds when everything it exports is a component, because the runtime can swap a component behind its registered identity. A constant, a helper function, a context object or a configuration map is different: **importers hold that value directly**. Re-running the module creates a new value that they never see, so self-accepting would leave them stale. So the tooling checks the module's exports on each update. When it finds a non-component export it cannot swap (some tooling escalates only when that exported value actually changed), it **hands the update on**, and HMR propagates to the module's importers: - If an importer is itself a component module, it becomes the boundary and runs again; the part of the tree it renders can be recreated and lose its state. - If the walk reaches the app's entry, the page **reloads**, and all state is gone. That is what the dashboard team sees. Edits to `filter-form.js` keep the typed values until someone adds `export const DEFAULT_RANGE = 30` beside the component; from then on, edits that touch it reset the form or reload the page. ## Limit 2: changes the instance cannot absorb Even in a file that exports only components, some edits cannot keep state: - Changing **which state hooks a component calls, or their order**, typically resets that component's state, because the stored values can no longer be matched to the new code. - **Module-scope variables** in the component file are initialised again on every swap, like those of any re-executed module, unless they are handed over explicitly. ## What survives where | Where the state lives | After a clean component swap | After escalation to the entry | |---|---|---| | component instance state | kept | lost with the reload | | module-scope variable in the edited file | reset | lost | | a store module that was not edited | kept | lost with the reload | The middle column explains a common surprise: a counter kept in a variable at the top of a component file resets on every edit, while the same counter kept in component state survives. ## Keeping edits hot The fixes follow from the mechanism: - Keep **one kind of export per file**: components in component files, and constants, helpers and context objects in modules of their own. - When one of those moved values is edited, the update climbs to the component modules that import it. Provided every importer is such a module, they are boundaries, so they re-render instead of the page reloading. - Keep state that must survive any edit in a module that rarely changes, such as a store, rather than at module scope in a component file. - When an edit unexpectedly resets state, check the file's exports before suspecting the framework: a single non-component export is the usual cause.

  • After the constant moves to its own module, what happens when someone edits that constant?
    The constant's module has no boundary of its own, so the update climbs to its importers. If every importer is a component module that accepts its own updates, they run again and re-render with the new value. The page does not reload, and only the state those modules own is at risk.
  • Which edits inside a file that exports only components still reset state?
    Changing which state hooks a component calls, or their order, typically resets that component's state, because the stored values can no longer be matched to the new code. Module-scope variables in the file are also initialised again on every swap, so state kept there is lost unless it is handed over explicitly.

  • State survives because the dev server snapshots the DOM and restores it
  • With hot swapping on, every edit to a component file keeps its state
  • Exporting a constant next to a component has no effect on hot swapping
  • Module-level variables in a component file survive a swap like component state