Skip to content

Commit 7e8a06c

Browse files
gaearonpoteto
andauthored
[Fresh] Always reset useMemoCache on Fast Refresh (facebook#30700)
Stacked on facebook#30662. Alternative to facebook#30663 and facebook#30677. During a Fast Refresh, we always want to evict the memo cache, same as we do with normal `useMemo`. The mechanism used by `useMemo` and other Hooks is this module-level variable: https://github.com/facebook/react/blob/fca5d655d78917400a2722287351c20938166669/packages/react-reconciler/src/ReactFiberHooks.js#L304-L307 which has DEV-only behavior as if the dependencies are always different: https://github.com/facebook/react/blob/fca5d655d78917400a2722287351c20938166669/packages/react-reconciler/src/ReactFiberHooks.js#L451-L460 The `useMemoCache` Hook doesn't use a dependency array but conceptually I think we want the same behavior. ## Test Plan The test passes. --------- Co-authored-by: Lauren Tan <poteto@users.noreply.github.com>
1 parent d9eb154 commit 7e8a06c

File tree

2 files changed

+2
-3
lines changed

2 files changed

+2
-3
lines changed

packages/react-reconciler/src/ReactFiberHooks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ function useMemoCache(size: number): Array<any> {
12261226
updateQueue.memoCache = memoCache;
12271227

12281228
let data = memoCache.data[memoCache.index];
1229-
if (data === undefined) {
1229+
if (data === undefined || (__DEV__ && ignorePreviousDependencies)) {
12301230
data = memoCache.data[memoCache.index] = new Array(size);
12311231
for (let i = 0; i < size; i++) {
12321232
data[i] = REACT_MEMO_CACHE_SENTINEL;

packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1637,8 +1637,7 @@ describe('ReactFreshIntegration', () => {
16371637
}
16381638
});
16391639

1640-
// eslint-disable-next-line jest/no-disabled-tests
1641-
it.skip('resets useMemoCache cache slots', async () => {
1640+
it('resets useMemoCache cache slots', async () => {
16421641
if (__DEV__) {
16431642
await render(`
16441643
const useMemoCache = require('react/compiler-runtime').c;

0 commit comments

Comments
 (0)