Skip to content

Commit

Permalink
Fix multiple roots
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Apr 7, 2022
1 parent 44bd862 commit a300efc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
11 changes: 10 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Expand Up @@ -401,6 +401,7 @@ let didScheduleUpdateDuringPassiveEffects = false;

const NESTED_PASSIVE_UPDATE_LIMIT = 50;
let nestedPassiveUpdateCount: number = 0;
let rootWithPassiveNestedUpdates: FiberRoot | null = null;

// If two updates are scheduled within the same event, we should treat their
// event times as simultaneous, even if the actual clock time has advanced
Expand Down Expand Up @@ -2214,6 +2215,7 @@ function commitRootImpl(
releaseRootPooledCache(root, remainingLanes);
if (__DEV__) {
nestedPassiveUpdateCount = 0;
rootWithPassiveNestedUpdates = null;
}
}

Expand Down Expand Up @@ -2481,7 +2483,12 @@ function flushPassiveEffectsImpl() {
// If additional passive effects were scheduled, increment a counter. If this
// exceeds the limit, we'll fire a warning.
if (didScheduleUpdateDuringPassiveEffects) {
nestedPassiveUpdateCount++;
if (root === rootWithPassiveNestedUpdates) {
nestedPassiveUpdateCount++;
} else {
nestedPassiveUpdateCount = 0;
rootWithPassiveNestedUpdates = root;
}
} else {
nestedPassiveUpdateCount = 0;
}
Expand Down Expand Up @@ -2760,6 +2767,8 @@ function checkForNestedUpdates() {
if (__DEV__) {
if (nestedPassiveUpdateCount > NESTED_PASSIVE_UPDATE_LIMIT) {
nestedPassiveUpdateCount = 0;
rootWithPassiveNestedUpdates = null;

console.error(
'Maximum update depth exceeded. This can happen when a component ' +
"calls setState inside useEffect, but useEffect either doesn't " +
Expand Down
11 changes: 10 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Expand Up @@ -401,6 +401,7 @@ let didScheduleUpdateDuringPassiveEffects = false;

const NESTED_PASSIVE_UPDATE_LIMIT = 50;
let nestedPassiveUpdateCount: number = 0;
let rootWithPassiveNestedUpdates: FiberRoot | null = null;

// If two updates are scheduled within the same event, we should treat their
// event times as simultaneous, even if the actual clock time has advanced
Expand Down Expand Up @@ -2214,6 +2215,7 @@ function commitRootImpl(
releaseRootPooledCache(root, remainingLanes);
if (__DEV__) {
nestedPassiveUpdateCount = 0;
rootWithPassiveNestedUpdates = null;
}
}

Expand Down Expand Up @@ -2481,7 +2483,12 @@ function flushPassiveEffectsImpl() {
// If additional passive effects were scheduled, increment a counter. If this
// exceeds the limit, we'll fire a warning.
if (didScheduleUpdateDuringPassiveEffects) {
nestedPassiveUpdateCount++;
if (root === rootWithPassiveNestedUpdates) {
nestedPassiveUpdateCount++;
} else {
nestedPassiveUpdateCount = 0;
rootWithPassiveNestedUpdates = root;
}
} else {
nestedPassiveUpdateCount = 0;
}
Expand Down Expand Up @@ -2760,6 +2767,8 @@ function checkForNestedUpdates() {
if (__DEV__) {
if (nestedPassiveUpdateCount > NESTED_PASSIVE_UPDATE_LIMIT) {
nestedPassiveUpdateCount = 0;
rootWithPassiveNestedUpdates = null;

console.error(
'Maximum update depth exceeded. This can happen when a component ' +
"calls setState inside useEffect, but useEffect either doesn't " +
Expand Down
Expand Up @@ -585,14 +585,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
'the number of nested updates to prevent infinite loops.',
);
}).toErrorDev(
gate(flags => flags.enableUseSyncExternalStoreShim)
? [
'The result of getSnapshot should be cached to avoid an infinite loop',
]
: [
'Maximum update depth exceeded.',
'The result of getSnapshot should be cached to avoid an infinite loop',
],
'The result of getSnapshot should be cached to avoid an infinite loop',
);
});

Expand Down

0 comments on commit a300efc

Please sign in to comment.