Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update on "Fix useMemoCache with setState in render"
Fixes the bug that alexmckenley and mofeiZ found where setState-in-render can reset useMemoCache and cause an infinite loop. The bug was that renderWithHooksAgain() was not resetting hook state when rerendering (so useMemo values were preserved) but was resetting the updateQueue. This meant that the entire memo cache was cleared on a setState-in-render.

The fix here is to call a new helper function to clear the update queue. It nulls out other properties, but for memoCache it just sets the index back to zero.

[ghstack-poisoned]
  • Loading branch information
josephsavona committed Sep 6, 2024
commit f6803f59f0b0490a75a8177059d71e59d07c73c0
5 changes: 5 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,11 @@ export function replaySuspendedComponentWithHooks<Props, SecondArg>(
ignorePreviousDependencies =
current !== null && current.type !== workInProgress.type;
}
// renderWithHooks only resets the updateQueue but does not clear it, since
// it needs to work for both this case (suspense replay) as well as for double
// renders in dev and setState-in-render. However, for the suspense replay case
// we need to reset the updateQueue to correctly handle unmount effects, so we
// clear the queue here
workInProgress.updateQueue = null;
const children = renderWithHooksAgain(
workInProgress,
Expand Down