Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 25 additions & 4 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ let pendingPassiveEffectsRemainingLanes: Lanes = NoLanes;
const NESTED_UPDATE_LIMIT = 50;
let nestedUpdateCount: number = 0;
let rootWithNestedUpdates: FiberRoot | null = null;
let isFlushingPassiveEffects = false;
let didScheduleUpdateDuringPassiveEffects = false;

const NESTED_PASSIVE_UPDATE_LIMIT = 50;
let nestedPassiveUpdateCount: number = 0;
Expand Down Expand Up @@ -522,6 +524,12 @@ export function scheduleUpdateOnFiber(
return null;
}

if (__DEV__) {
if (isFlushingPassiveEffects) {
didScheduleUpdateDuringPassiveEffects = true;
}
}

// Mark that the root has a pending update.
markRootUpdated(root, lane, eventTime);

Expand Down Expand Up @@ -2204,6 +2212,9 @@ function commitRootImpl(
// There were no passive effects, so we can immediately release the cache
// pool for this render.
releaseRootPooledCache(root, remainingLanes);
if (__DEV__) {
nestedPassiveUpdateCount = 0;
}
}

// Read this again, since an effect might have updated it
Expand Down Expand Up @@ -2420,6 +2431,9 @@ function flushPassiveEffectsImpl() {
}

if (__DEV__) {
isFlushingPassiveEffects = true;
didScheduleUpdateDuringPassiveEffects = false;

if (enableDebugTracing) {
logPassiveEffectsStarted(lanes);
}
Expand Down Expand Up @@ -2463,10 +2477,17 @@ function flushPassiveEffectsImpl() {

flushSyncCallbacks();

// If additional passive effects were scheduled, increment a counter. If this
// exceeds the limit, we'll fire a warning.
nestedPassiveUpdateCount =
rootWithPendingPassiveEffects === null ? 0 : nestedPassiveUpdateCount + 1;
if (__DEV__) {
// If additional passive effects were scheduled, increment a counter. If this
// exceeds the limit, we'll fire a warning.
if (didScheduleUpdateDuringPassiveEffects) {
nestedPassiveUpdateCount++;
} else {
nestedPassiveUpdateCount = 0;
}
isFlushingPassiveEffects = false;
didScheduleUpdateDuringPassiveEffects = false;
}

// TODO: Move to commitPassiveMountEffects
onPostCommitRootDevTools(root);
Expand Down
29 changes: 25 additions & 4 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ let pendingPassiveEffectsRemainingLanes: Lanes = NoLanes;
const NESTED_UPDATE_LIMIT = 50;
let nestedUpdateCount: number = 0;
let rootWithNestedUpdates: FiberRoot | null = null;
let isFlushingPassiveEffects = false;
let didScheduleUpdateDuringPassiveEffects = false;

const NESTED_PASSIVE_UPDATE_LIMIT = 50;
let nestedPassiveUpdateCount: number = 0;
Expand Down Expand Up @@ -522,6 +524,12 @@ export function scheduleUpdateOnFiber(
return null;
}

if (__DEV__) {
if (isFlushingPassiveEffects) {
didScheduleUpdateDuringPassiveEffects = true;
}
}

// Mark that the root has a pending update.
markRootUpdated(root, lane, eventTime);

Expand Down Expand Up @@ -2204,6 +2212,9 @@ function commitRootImpl(
// There were no passive effects, so we can immediately release the cache
// pool for this render.
releaseRootPooledCache(root, remainingLanes);
if (__DEV__) {
nestedPassiveUpdateCount = 0;
}
}

// Read this again, since an effect might have updated it
Expand Down Expand Up @@ -2420,6 +2431,9 @@ function flushPassiveEffectsImpl() {
}

if (__DEV__) {
isFlushingPassiveEffects = true;
didScheduleUpdateDuringPassiveEffects = false;

if (enableDebugTracing) {
logPassiveEffectsStarted(lanes);
}
Expand Down Expand Up @@ -2463,10 +2477,17 @@ function flushPassiveEffectsImpl() {

flushSyncCallbacks();

// If additional passive effects were scheduled, increment a counter. If this
// exceeds the limit, we'll fire a warning.
nestedPassiveUpdateCount =
rootWithPendingPassiveEffects === null ? 0 : nestedPassiveUpdateCount + 1;
if (__DEV__) {
// If additional passive effects were scheduled, increment a counter. If this
// exceeds the limit, we'll fire a warning.
if (didScheduleUpdateDuringPassiveEffects) {
nestedPassiveUpdateCount++;
} else {
nestedPassiveUpdateCount = 0;
}
isFlushingPassiveEffects = false;
didScheduleUpdateDuringPassiveEffects = false;
}

// TODO: Move to commitPassiveMountEffects
onPostCommitRootDevTools(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,10 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
'calls setState inside componentWillUpdate or componentDidUpdate. React limits ' +
'the number of nested updates to prevent infinite loops.',
);
}).toErrorDev(
}).toErrorDev([
'Maximum update depth exceeded.',
'The result of getSnapshot should be cached to avoid an infinite loop',
);
]);
});

test('getSnapshot can return NaN without infinite loop warning', async () => {
Expand Down