Skip to content

Commit c3c4c3f

Browse files
committed
Fix useMutableSource tearing bug
Fix is to move the entanglement call outside of the block that checks if the snapshot has changed.
1 parent 43ce30b commit c3c4c3f

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

packages/react-reconciler/src/ReactFiberHooks.new.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -997,14 +997,11 @@ function useMutableSource<Source, Snapshot>(
997997
const suspenseConfig = requestCurrentSuspenseConfig();
998998
const lane = requestUpdateLane(fiber, suspenseConfig);
999999
markRootMutableRead(root, lane);
1000-
1001-
// If the source mutated between render and now,
1002-
// there may be state updates already scheduled from the old source.
1003-
// Entangle the updates so that they render in the same batch.
1004-
// TODO: I think we need to entangle even if the snapshot matches,
1005-
// because there could have been an update to a different hook.
1006-
markRootEntangled(root, root.mutableReadLanes);
10071000
}
1001+
// If the source mutated between render and now,
1002+
// there may be state updates already scheduled from the old source.
1003+
// Entangle the updates so that they render in the same batch.
1004+
markRootEntangled(root, root.mutableReadLanes);
10081005
}
10091006
}, [getSnapshot, source, subscribe]);
10101007

packages/react-reconciler/src/ReactFiberHooks.old.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -986,15 +986,14 @@ function useMutableSource<Source, Snapshot>(
986986
suspenseConfig,
987987
);
988988
setPendingExpirationTime(root, expirationTime);
989-
990-
// If the source mutated between render and now,
991-
// there may be state updates already scheduled from the old getSnapshot.
992-
// Those updates should not commit without this value.
993-
// There is no mechanism currently to associate these updates though,
994-
// so for now we fall back to synchronously flushing all pending updates.
995-
// TODO: Improve this later.
996-
markRootExpiredAtTime(root, getLastPendingExpirationTime(root));
997989
}
990+
// If the source mutated between render and now,
991+
// there may be state updates already scheduled from the old getSnapshot.
992+
// Those updates should not commit without this value.
993+
// There is no mechanism currently to associate these updates though,
994+
// so for now we fall back to synchronously flushing all pending updates.
995+
// TODO: Improve this later.
996+
markRootExpiredAtTime(root, getLastPendingExpirationTime(root));
998997
}
999998
}, [getSnapshot, source, subscribe]);
1000999

0 commit comments

Comments
 (0)