Fast Refresh doesn't reset the React Compiler memo cache from c()
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- react, typescript
- Lĩnh vực
- frontend
Hướng nghiên cứu
Bắt đầu với src/react/compiler-runtime.ts và kiểm tra cách c() lưu bộ nhớ đệm memo; so sánh với cách xử lý refresh trong src/dom/dispatcher.ts. Thêm trường hợp tái hiện vào tests/fast-refresh.test.tsx và xác minh thành phần đã cập nhật hiển thị after:1, rồi after:2, trong khi vẫn giữ nguyên state. Issue báo cáo rằng bài kiểm tra này và toàn bộ bộ kiểm thử đều chạy qua với thay đổi được đề xuất.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- TypeScript
- Star
- 408
- Fork
- 13
- Merge trung bình
- 1 giờ 44 phút
- Pull request đã merge (30 ngày)
- 21
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TanStack/redact
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100
TanStack/redact#67 · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của TanStack/redact
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
marmelab/react-admin#11431 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
db-ux-design-system/core-web#8655 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
stripe/react-stripe-js#728 ·
-
[Upload Grade C dataset] the pop-up changes it's size while uploading the document => Jumping effectĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
epam/statgpt-admin-frontend#305 ·
Maintainer thường phản hồi trong vòng 1 ngày