Fast Refresh state reset after editing unrelated UI text
0 reputation · 11 Jun 2024, 14:25 UTC
0 reputation · 11 Jun 2024, 14:25 UTC
Developers using React Native Fast Refresh expect that editing UI text unrelated to a component’s state leaves useState values intact. In practice, the state occasionally resets to its initial value after such edits, creating confusion about what actually changed. The inconsistency appears to be influenced by Metro’s module caching, the presence of custom Babel plugins, and variations between React Native releases and Expo managed versus bare workflows. Because the Fast Refresh reconciliation step may fail to map the updated component tree under certain configurations, it is unclear which combination of factors triggers the unintended state reset.
What specific Metro cache conditions cause Fast Refresh to discard useState when only static text is modified? How do custom Babel plugins or non‑default Metro settings interfere with the state‑preservation reconciliation step? Under which React Native version ranges does this behavior become reproducible?
31775 reputation · 11 Jun 2024, 16:11 UTC
Fast Refresh does not decide whether to keep useState based on which line you edited. It decides based on whether the module still "looks like" a pure component module and whether the component's identity survives the update. If a text-only edit causes a state reset, the text itself is almost never the cause — the file was already in a state where any edit triggers a full reload, and the text change was just the first edit you made.
Confirmed, well-established behavior:
react-refresh/babel) that registers component signatures. Anything that interferes with that transform breaks the signature map.Likely but not confirmed for your setup: Metro cache corruption, custom Babel plugins, or a version-specific regression. These are plausible contributors, but they are diagnoses to verify, not established causes. There is no documented "Metro cache condition" that selectively discards state on text edits — a poisoned cache typically causes stale modules or failed refreshes broadly, not surgical state loss.
export const SOME_CONST, a navigation helper, a styles object), that alone explains the reset. Move the non-component export to its own file.npx react-native start --reset-cache or on Expo: npx expo start -c If the reset disappears after a cache clear but returns later, note what changed in between (dependency installs, Babel config edits).babel.config.js includes custom plugins (codegen, module-resolver with unusual aliasing, anything injecting identifiers), disable them one at a time and re-test. Plugins that wrap or rename component functions prevent the React Refresh transform from registering a stable signature.Fast Refresh has been the default since React Native 0.61, and the core eligibility rules above have been stable since. Specific reconciliation bugs have appeared and been fixed across individual releases, but there is no verified version range that reproducibly resets state on static-text edits — treat any such claim with skepticism and pin it to a reproducible example before trusting it. Behavior also differs between Expo managed and bare workflows because they ship different Metro and Babel presets, so always report both your RN/Expo SDK version and workflow type when comparing notes.
Does the affected file export anything other than the component? If yes, that is the answer and no cache or plugin investigation is needed. If no, the next diagnostic is whether Metro logs a Fast Refresh or a full reload on the edit — share that and the Babel config.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.