Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useMemoCache with setState in render #30889

Merged
merged 12 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,9 @@ function renderWithHooksAgain<Props, SecondArg>(
currentHook = null;
workInProgressHook = null;

workInProgress.updateQueue = null;
if (workInProgress.updateQueue != null) {
resetFunctionComponentUpdateQueue(workInProgress.updateQueue);
}

if (__DEV__) {
// Also validate hook order for cascading updates.
Expand Down Expand Up @@ -1101,6 +1103,22 @@ if (enableUseMemoCacheHook) {
};
}

function resetFunctionComponentUpdateQueue(
updateQueue: FunctionComponentUpdateQueue,
): void {
updateQueue.lastEffect = null;
updateQueue.events = null;
updateQueue.stores = null;
if (enableUseMemoCacheHook) {
if (updateQueue.memoCache != null) {
// NOTE: this function intentionally does not reset memoCache data. We reuse updateQueue for the memo
// cache to avoid increasing the size of fibers that don't need a cache, but we don't want to reset
// the cache when other properties are reset.
updateQueue.memoCache.index = 0;
}
}
}

function useThenable<T>(thenable: Thenable<T>): T {
// Track the position of the thenable within this fiber.
const index = thenableIndexCounter;
Expand Down
39 changes: 13 additions & 26 deletions packages/react-reconciler/src/__tests__/useMemoCache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ let assertLog;
let useMemo;
let useState;
let useMemoCache;
let waitForThrow;
let MemoCacheSentinel;
let ErrorBoundary;

Expand All @@ -32,7 +31,6 @@ describe('useMemoCache()', () => {
useMemo = React.useMemo;
useMemoCache = require('react/compiler-runtime').c;
useState = React.useState;
waitForThrow = require('internal-test-utils').waitForThrow;
MemoCacheSentinel = Symbol.for('react.memo_cache_sentinel');

class _ErrorBoundary extends React.Component {
Expand Down Expand Up @@ -630,18 +628,9 @@ describe('useMemoCache()', () => {

// Finish loading the data.
await act(() => updatedChunkB.resolve('B2'));
if (gate(flags => flags.enableNoCloningMemoCache)) {
// We did not have process the first chunk again. We reused the
// computation from the earlier attempt.
assertLog([]);
} else {
// Because we clone/reset the memo cache after every aborted attempt, we
// must process the first chunk again.
//
// That's three total times we've processed the first chunk, compared to
// just once when enableNoCloningMemoCache is on.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this reset was just an artifact of the renderWithHooksAgain() reset. so the feature flag helps, but a bit less than we expected (we process the chunk once instead of twice)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm nope, more complicated than that. Tests seem to be failing on persistent mode with this change, still debugging.

assertLog(['Some expensive processing... [A2]']);
}
// We did not have process the first chunk again. We reused the
// computation from the earlier attempt.
assertLog([]);
expect(root).toMatchRenderedOutput(
<>
<div>Input: hi!</div>
Expand All @@ -667,7 +656,7 @@ describe('useMemoCache()', () => {
}

// Baseline / source code
function useUserMemo(value) {
function useManualMemo(value) {
return useMemo(() => [value], [value]);
}

Expand All @@ -683,24 +672,22 @@ describe('useMemoCache()', () => {
}

/**
* Test case: note that the initial render never completes
* Test with useMemoCache
*/
let root = ReactNoop.createRoot();
const IncorrectInfiniteComponent = makeComponent(useCompilerMemo);
root.render(<IncorrectInfiniteComponent value={2} />);
await waitForThrow(
'Too many re-renders. React limits the number of renders to prevent ' +
'an infinite loop.',
);
const CompilerMemoComponent = makeComponent(useCompilerMemo);
await act(() => {
root.render(<CompilerMemoComponent value={2} />);
});
expect(root).toMatchRenderedOutput(<div>2</div>);

/**
* Baseline test: initial render is expected to complete after a retry
* (triggered by the setState)
* Test with useMemo
*/
root = ReactNoop.createRoot();
const CorrectComponent = makeComponent(useUserMemo);
const HookMemoComponent = makeComponent(useManualMemo);
await act(() => {
root.render(<CorrectComponent value={2} />);
root.render(<HookMemoComponent value={2} />);
});
expect(root).toMatchRenderedOutput(<div>2</div>);
});
Expand Down
Loading