Repository navigation
Description
A component written inline in a HOC call, such as memo(function App() { ... }), loses its state each time Fast Refresh runs its module again, even if the module's code did not change. Two things must come together: the component calls a custom Hook that is imported by name, and a Babel transform rewrites the import references, as @babel/plugin-transform-modules-commonjs does. There is no warning.
React version: react-refresh 0.19.0 (latest; same on 0.14.2 and main at cb7c59e), react and react-dom 19.3.1. With React 19.2.9 a memo or forwardRef component is kept, see below.
Steps To Reproduce
- In an empty folder run
npm init -yandnpm install @babel/core@7.29.13 @babel/plugin-transform-modules-commonjs@7.29.7 jsdom@31.0.0 react@19.3.1 react-dom@19.3.1 react-refresh@0.19.0. - Save the code below as
repro.jsand runnode repro.js.
Link to code example:
const babel = require('@babel/core');
const {JSDOM} = require('jsdom');
const RefreshRuntime = require('react-refresh/runtime');
global.window = new JSDOM('<div id="root"></div>').window;
global.document = window.document;
global.IS_REACT_ACT_ENVIRONMENT = true;
// Must run before React DOM loads.
RefreshRuntime.injectIntoGlobalHook(global);
const React = require('react');
const {createRoot} = require('react-dom/client');
const source = `
import {createElement, memo} from 'react';
import {useFancyState} from './hooks';
export function Plain() {
useFancyState();
return createElement('p', null, 'Plain');
}
export const Wrapped = memo(function Wrapped() {
useFancyState();
return createElement('p', null, 'Wrapped');
});
`;
const {code} = babel.transformSync(source, {
babelrc: false,
configFile: false,
plugins: [
[require('react-refresh/babel'), {skipEnvCheck: true}],
'@babel/plugin-transform-modules-commonjs',
],
});
console.log(code);
// Run the compiled module the way a bundler does for a hot update.
const modules = {react: React, './hooks': {useFancyState: () => React.useState(0)}};
function runModule() {
const exports = {};
new Function('require', 'exports', '$RefreshReg$', '$RefreshSig$', code)(
id => modules[id],
exports,
RefreshRuntime.register,
RefreshRuntime.createSignatureFunctionForTransform,
);
return exports;
}
(async () => {
const {Plain, Wrapped} = runModule();
const container = document.getElementById('root');
const root = createRoot(container);
await React.act(() => {
root.render(
React.createElement(
'div',
null,
React.createElement(Plain),
React.createElement(Wrapped),
),
);
});
const [plain, wrapped] = container.querySelectorAll('p');
// The same code again: a hot update that changed nothing.
runModule();
await React.act(() => {
RefreshRuntime.performReactRefresh();
});
const [nextPlain, nextWrapped] = container.querySelectorAll('p');
console.log(`\nReact ${React.version}, after a refresh with unchanged code:`);
console.log(` Plain: ${nextPlain === plain ? 'kept' : 'REMOUNTED'}`);
console.log(` Wrapped: ${nextWrapped === wrapped ? 'kept' : 'REMOUNTED'}`);
})();The current behavior
The script prints
React 19.3.1, after a refresh with unchanged code:
Plain: kept
Wrapped: REMOUNTED
Wrapped has a new DOM node. The compiled module has two signatures for it. The inner one is right. The one around memo() returns a name that does not exist in the output:
const Wrapped = exports.Wrapped = _s2((0, _react.memo)(_c2 = _s2(function Wrapped() {
_s2();
(0, _hooks.useFancyState)();
return (0, _react.createElement)('p', null, 'Wrapped');
}, "elKi182M2Apzat70hjwJyknkHLQ=", false, function () {
return [_hooks.useFancyState];
})), "elKi182M2Apzat70hjwJyknkHLQ=", false, function () {
return [useFancyState];
});Calling it throws a ReferenceError. computeFullKey in the runtime catches the error and sets forceReset, so the family of the memo type is stale on every refresh.
The cause is in the Babel plugin. getHookCallsSignature clones the custom Hook nodes once per function, and createArgumentsForSignature then puts the same node objects into the signature of the function and into the signature of each HOC call around it. Babel's module transform rewrites an import reference node only once (it keeps a seen set of nodes), so the second place keeps the bare identifier. #16019 fixed the same problem for the single signature. The signatures for HOC calls came later, in #21104 (react-refresh 0.10.0).
The outer signature has been wrong since then. For memo and forwardRef it caused no remount until #36950 (React 19.3.0), which remounts a fiber whose outer family is stale: with react and react-dom 19.2.9 the script prints Wrapped: kept, with 19.3.0 and 19.3.1 it prints REMOUNTED. A HOC that returns a function of its own, as in hoc(function App() { ... }), remounts with 19.2.9 as well.
Other setups:
- Babel 8.1.2 compiles the module to the same output.
- React Native: the same module through
@react-native/babel-preset0.88.0 with its HMR config (react-refresh 0.14.2) compiles to the samereturn [useFancyState]. Checked on the transform output only, not in a running app. - Expo SDK 58 (React Native 0.88.1, React 19.3.1, react-refresh 0.14.2), in a real app on an Android emulator: on the screen measured, mounted components of this kind remounted on each Fast Refresh that ran their module again. There the outer signature returns the module object instead of the Hook, and the runtime resets because the entry is not a function. iOS not checked.
The expected behavior
Wrapped keeps its state like Plain. Both signatures return [_hooks.useFancyState].
AI disclosure: Claude Code Fable 5.2 found and analysed this and drafted the text. I ran the script above myself.
Activity
xgauravyaduvanshii commentedon Nov 18, 2026
@/tmp/gh-c38229.txt