@samitouri / QOS-React / commits / 7e8a06cf4c

[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 <poteto@users.noreply.github.com>

dan committed Aug 15, 2024 at 01:02 UTC 7e8a06cf4c628be45171da52c1a8e97f9869b7ee
2 files changed +2 -3
packages/react-reconciler/src/ReactFiberHooks.js
+1 -1
@@ -1226,7 +1226,7 @@ function useMemoCache(size: number): Array<any> {
1226 updateQueue.memoCache = memoCache;
1227
1228 let data = memoCache.data[memoCache.index];
1229 - if (data === undefined) {
1229 + if (data === undefined || (__DEV__ && ignorePreviousDependencies)) {
1230 data = memoCache.data[memoCache.index] = new Array(size);
1231 for (let i = 0; i < size; i++) {
1232 data[i] = REACT_MEMO_CACHE_SENTINEL;
packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js
+1 -2
@@ -1637,8 +1637,7 @@ describe('ReactFreshIntegration', () => {
1637 }
1638 });
1639
1640 - // eslint-disable-next-line jest/no-disabled-tests
1641 - it.skip('resets useMemoCache cache slots', async () => {
1640 + it('resets useMemoCache cache slots', async () => {
1641 if (__DEV__) {
1642 await render(`
1643 const useMemoCache = require('react/compiler-runtime').c;