From 7e8a06cf4c628be45171da52c1a8e97f9869b7ee Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 15 Aug 2024 01:02:29 +0100 Subject: [PATCH] [Fresh] Always reset useMemoCache on Fast Refresh (#30700) Stacked on https://github.com/facebook/react/pull/30662. Alternative to https://github.com/facebook/react/pull/30663 and https://github.com/facebook/react/pull/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 --- packages/react-reconciler/src/ReactFiberHooks.js | 2 +- .../react-refresh/src/__tests__/ReactFreshIntegration-test.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberHooks.js b/packages/react-reconciler/src/ReactFiberHooks.js index f72056f674..abe1516520 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.js +++ b/packages/react-reconciler/src/ReactFiberHooks.js @@ -1226,7 +1226,7 @@ function useMemoCache(size: number): Array { updateQueue.memoCache = memoCache; let data = memoCache.data[memoCache.index]; - if (data === undefined) { + if (data === undefined || (__DEV__ && ignorePreviousDependencies)) { data = memoCache.data[memoCache.index] = new Array(size); for (let i = 0; i < size; i++) { data[i] = REACT_MEMO_CACHE_SENTINEL; diff --git a/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js b/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js index 2115a15f67..cc9deb6236 100644 --- a/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js +++ b/packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js @@ -1637,8 +1637,7 @@ describe('ReactFreshIntegration', () => { } }); - // eslint-disable-next-line jest/no-disabled-tests - it.skip('resets useMemoCache cache slots', async () => { + it('resets useMemoCache cache slots', async () => { if (__DEV__) { await render(` const useMemoCache = require('react/compiler-runtime').c;