Repository navigation
fix: run style interpolations on every render (#5788) - #5809
Conversation
The render cache added in 6.4.0 reused a cached result on a props-equal re-render, skipping evaluation of interpolations and attr functions. Those run in the component's own render body, so skipping them dropped any hook they called (crashing with "Rendered fewer hooks than expected", in production too) and served a stale class name or style object when an interpolation read anything outside props and theme. Interpolations and attr functions now run on every render, on both the web and native paths. The pure work downstream of the produced CSS (hashing, compile, injection on web; the CSS-to-style-object transform on native) is still memoized, so repeat renders that yield the same styles stay cheap. A props-equal re-render bailout belongs at the component boundary via React.memo, which only the caller can key correctly.
cssToStyleObject keyed its cache by generateComponentId(flatCSS), whose base-52 name conversion and regex ran on every render and dominated the native re-render cost (~80% of per-child work, measured), while the id was only ever used as an internal Map key and never as a class name. Keying by the flat CSS string directly drops generateComponentId from the path. Recovers most of the re-render cost from running interpolations every render (#5788) and makes cache-miss, cycling, and sibling paths faster than before, since the id was on the miss path too. A string key also cannot collide the way the 32-bit hash could, so two distinct CSS strings can no longer return each other's cached style.
… text Extract the React use*-recording harness (duplicated across the web hook tests and the new native one) into src/test/recordHooks.ts, and update the tests, docs, and agent rules that still described the removed render cache: retitle the HMR class-name test, rewrite the memoization suite's header and hook-sequence test to re-render vocabulary, correct the runtime-performance hot-path wording, and fix an AGENTS rule that read as "hooks must be unconditional" against the deliberate __SERVER__/IS_RSC gating. Trim the changeset to user-observable behavior.
🦋 Changeset detectedLatest commit: 8697b33 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
ℹ️ No critical issues — the fix is correct, complete, and well-tested. One defer-able suggestion inline and one cleanup nit.
Reviewed changes
- Web render path (
StyledComponent.ts) — removed theuseRef<RenderCache>+shallowEqualContextshortcut entirely;resolveContext+generateAndInjectStylesnow run unconditionally on every render. Downstream memoization (dynamicNameCachekeyed by CSS string,hasNameForId) keeps repeat renders cheap, andReact.memois correctly documented as the caller-side bailout. - Native render path (
StyledNativeComponent.ts+InlineStyle.ts) — same render-cache removal, plus thecssToStyleObjectcache re-keyed from a 32-bit hash-derived name (where a collision served one distinct CSS string's style object to another) to a module-levelMapkeyed by the flat CSS string. This both fixes the collision and — because string-keyed equality now means identity — letsgenerateStyleObjectreturn a stable reference, so the pre-existinguseMemo([props.style, generatedStyles])keeps a stable style-array identity without an outer cache. - Tests — the
recordHookshelper (wraps every Reactuse*export, restored infinally) is extracted and shared between web and native. The new web + native tests assert identical hook sequences across mount/same-props/changed-props renders, pin auseContextinside an interpolation to every render, and pin class/style recomputation when an interpolation reads external state with identical props. Positive controls on the recording are preserved. The HMRcreateThemetest now asserts class-name stability across aComponentStyleswap rather than cache behavior. - Docs/AGENTS.md/changeset — the rewritten rule, the updated
docs/runtime-performance.mdhot-path section, and the patch changeset are all accurate against the code and consumer-facing without internal names.
I verified the full web suite (59 suites / 927 tests), the native suite, and tsc --noEmit all pass, and ran the web bench (no hot-path pathology). Two independent red-team passes found no remaining render path where a styled component could emit a stale class/style array or a divergent hook sequence — every remaining memoization layer is keyed strictly on produced CSS content, so a hit always implies identical CSS was already injected, and the hook sequence is now unconditional across web, native, SSR, RSC, and HMR.
ℹ️ Nitpicks
StyledComponent.ts:40—declare const __SERVER__;is now orphaned: its only in-file use was the removed render-cache branch (I grepped). It's inert type-only code, so this is cosmetic, but the same edit that deleted the cache could have dropped it too.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
| } | ||
|
|
||
| let generated: Dict<any> = {}; | ||
| let generated = new Map<string, any>(); |
There was a problem hiding this comment.
Hardly a blocker, but the native CSS-to-style cache now retains every distinct flat CSS string (plus its style object) for the life of the module — resetStyleCache is the only relief. The growth profile is the same as the old hash-keyed cache (also unbounded), but the key payload is bigger and the other half of this PR (interpolations re-evaluate every render) raises the entry-add rate for fully-dynamic styles. The web mirror already solved this exact class of leak with MAX_DYNAMIC_NAME_CACHE (FIFO eviction, aligned to warnTooManyClasses) — worth considering whether the native map deserves an equivalent cap, or at minimum a note in docs/runtime-performance.md alongside the web one. Details

Fixes #5788.
Upgrading past 6.3.12 crashed any component that calls a React hook, or reads a value outside its props and theme, from inside a style interpolation, with "Rendered fewer hooks than expected", in both development and production.
@mui/styled-engine-scwith MUI X DataGrid hit this on mount. The same components could also render with stale styles. It was a regression introduced in 6.4.0.What changes
React.memoto skip those re-renders.Thanks to @stuaylward for the clear report and the version bisect that pinned the regression.
The same fix applies to v7 on
main; I'll open that separately.