Fast Refresh doesn't reset the React Compiler memo cache from c()
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 82/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- react, typescript
- 領域
- frontend
調査の方向性
src/react/compiler-runtime.tsから始めて、c()がメモキャッシュをどのように保存するかを調べ、src/dom/dispatcher.tsでのrefresh処理と比較してください。再現ケースをtests/fast-refresh.test.tsxに追加し、状態を保持したまま更新後のコンポーネントがafter:1、続いてafter:2を表示することを確認してください。Issueによると、提案された変更でこのテストと全テストスイートが通ります。
索引モデルが issue の本文から書いたものです。
説明
Summary
After a Fast Refresh update, components compiled by React Compiler keep rendering their old output. The compiler runtime's c() keeps its memo cache across the refresh, so cached JSX from the previous version of the component is returned until a full reload.
React resets this cache on refresh. In development, renderWithHooks sets ignorePreviousDependencies when the fiber's type changed, and useMemoCache then refills the cache with sentinels (ReactFiberHooks.js).
Reproduction
Added to tests/fast-refresh.test.tsx. The component is written the way the compiler emits it:
import { c } from 'react/compiler-runtime'
it('resets the React Compiler memo cache but preserves state', () => {
function version(label: string) {
return function Example() {
const $ = c(3)
const [count, setCount] = useState(0)
let t0
if ($[0] === Symbol.for('react.memo_cache_sentinel')) {
t0 = () => setCount(n => n + 1)
$[0] = t0
} else t0 = $[0] as () => void
let t1
if ($[1] !== count) {
t1 = <button onClick={t0}>{label}:{count}</button>
$[1] = count
$[2] = t1
} else t1 = $[2]
return t1
}
}
const Before = version('before'), update = family(Before, 'stable')
const { container } = setup(<Before />)
click(container)
update(version('after'))
Refresh.performReactRefresh()
expect(container.textContent).toBe('after:1')
click(container)
expect(container.textContent).toBe('after:2')
})
On main it fails with expected 'before:1' to be 'after:1'.
Cause
c() in src/react/compiler-runtime.ts stores the cache in a useRef, which survives the refresh. useMemo and useCallback already handle this case: depsEqual returns false while the fiber is refreshing.
Babel's compiler hides the bug in development because it also emits a source-hash check that clears the cache whenever the file changes. The Rust port in oxc-transform-react, which @vitejs/plugin-react uses for react({ compiler: true }), doesn't emit that check, so with it every hot update of a compiled component is stale.
Suggested fix
Back c() with useMemo, so it picks up the existing refresh handling in depsEqual:
export function c(size: number): Array<unknown> {
return useMemo(() => new Array<unknown>(size).fill(MEMO_CACHE_SENTINEL), [size])
}
With this change the test above passes and the rest of the suite still passes (1860 passed, 91 skipped).
Versions
@tanstack/redact0.2.0, andmainat d56d87cbreact-refresh0.19.0 (from the test suite)oxc-transform-react0.155.0 with@vitejs/plugin-react6.1.2, where we first hit it
Disclaimer: this issue, including the reproduction test and the suggested fix, was researched and written by Claude, an AI assistant from Anthropic.
- 主要言語
- TypeScript
- スター
- 408
- フォーク
- 13
- 平均マージ
- 1時間 44分
- マージ済み PR(30日)
- 21
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
TanStack/redact のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
TanStack/redact#67 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
TanStack/redact の issue をすべて見る
似ている issue
-
Parallel reticle_verify opens per-flow leases without the source lease's ignoreHTTPSErrors対応中かも @as992949791 が今日担当しました。 オープンgood first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
reticlehq/reticle#2033 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
area:quality p3-medium ready-for-agent type:docs
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
fderuiter/deruiter.dev#3589 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
manfredsteyer/angular-oauth2-oidc#1514 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
marmelab/react-admin#11431 ·
メンテナーはふだん 1 日以内に返信