Skip to content

Commit 23ed7bf

Browse files
committed
Remove warning for dangling passive effects
In legacy mode, a test can get into a situation where passive effects are "dangling" — an update finished, and scheduled some passive effects, but the effects don't flush. This is why React warns if you don't wrap updates in act. The act API is responsible for flushing passive effects. But there are some cases where the act API (in legacy roots) intentionally doesn't warn, like updates that originate from roots and classes. It's possible those updates will render children that contain useEffect. Because of this, dangling effects are still possible, and React doesn't warn about it. So we implemented a second act warning for dangling effects. However, in concurrent roots, we now enforce that all APIs that schedule React work must be wrapped in act. There's no scenario where dangling passive effects can happen that doesn't already trigger the warning for updates. So the dangling effects warning is redundant. The warning was never part of a public release. It was only enabled in concurrent roots. So we can delete it.
1 parent 1961c99 commit 23ed7bf

File tree

5 files changed

+2
-113
lines changed

5 files changed

+2
-113
lines changed

packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js

-31
Original file line numberDiff line numberDiff line change
@@ -98,43 +98,12 @@ describe('ReactTestUtils.act()', () => {
9898
}).toErrorDev([]);
9999
});
100100

101-
it('warns in strict mode', () => {
102-
expect(() => {
103-
ReactDOM.render(
104-
<React.StrictMode>
105-
<App />
106-
</React.StrictMode>,
107-
document.createElement('div'),
108-
);
109-
}).toErrorDev([
110-
'An update to App ran an effect, but was not wrapped in act(...)',
111-
]);
112-
});
113-
114101
// @gate __DEV__
115102
it('does not warn in concurrent mode', () => {
116103
const root = ReactDOM.createRoot(document.createElement('div'));
117104
act(() => root.render(<App />));
118105
Scheduler.unstable_flushAll();
119106
});
120-
121-
it('warns in concurrent mode if root is strict', () => {
122-
// TODO: We don't need this error anymore in concurrent mode because
123-
// effects can only be scheduled as the result of an update, and we now
124-
// enforce all updates must be wrapped with act, not just hook updates.
125-
expect(() => {
126-
const root = ReactDOM.createRoot(document.createElement('div'), {
127-
unstable_strictMode: true,
128-
});
129-
root.render(<App />);
130-
}).toErrorDev(
131-
'An update to Root inside a test was not wrapped in act(...)',
132-
{withoutStack: true},
133-
);
134-
expect(() => Scheduler.unstable_flushAll()).toErrorDev(
135-
'An update to App ran an effect, but was not wrapped in act(...)',
136-
);
137-
});
138107
});
139108
});
140109

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

-7
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ import {
8282
scheduleUpdateOnFiber,
8383
requestUpdateLane,
8484
requestEventTime,
85-
warnIfNotCurrentlyActingEffectsInDEV,
8685
markSkippedUpdateLanes,
8786
isInterleavedUpdate,
8887
} from './ReactFiberWorkLoop.new';
@@ -1676,9 +1675,6 @@ function mountEffect(
16761675
create: () => (() => void) | void,
16771676
deps: Array<mixed> | void | null,
16781677
): void {
1679-
if (__DEV__) {
1680-
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
1681-
}
16821678
if (
16831679
__DEV__ &&
16841680
enableStrictEffects &&
@@ -1704,9 +1700,6 @@ function updateEffect(
17041700
create: () => (() => void) | void,
17051701
deps: Array<mixed> | void | null,
17061702
): void {
1707-
if (__DEV__) {
1708-
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
1709-
}
17101703
return updateEffectImpl(PassiveEffect, HookPassive, create, deps);
17111704
}
17121705

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

-7
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ import {
8282
scheduleUpdateOnFiber,
8383
requestUpdateLane,
8484
requestEventTime,
85-
warnIfNotCurrentlyActingEffectsInDEV,
8685
markSkippedUpdateLanes,
8786
isInterleavedUpdate,
8887
} from './ReactFiberWorkLoop.old';
@@ -1676,9 +1675,6 @@ function mountEffect(
16761675
create: () => (() => void) | void,
16771676
deps: Array<mixed> | void | null,
16781677
): void {
1679-
if (__DEV__) {
1680-
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
1681-
}
16821678
if (
16831679
__DEV__ &&
16841680
enableStrictEffects &&
@@ -1704,9 +1700,6 @@ function updateEffect(
17041700
create: () => (() => void) | void,
17051701
deps: Array<mixed> | void | null,
17061702
): void {
1707-
if (__DEV__) {
1708-
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
1709-
}
17101703
return updateEffectImpl(PassiveEffect, HookPassive, create, deps);
17111704
}
17121705

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

+1-34
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,7 @@ import {
9797
createWorkInProgress,
9898
assignFiberPropertiesInDEV,
9999
} from './ReactFiber.new';
100-
import {
101-
NoMode,
102-
StrictLegacyMode,
103-
ProfileMode,
104-
ConcurrentMode,
105-
} from './ReactTypeOfMode';
100+
import {NoMode, ProfileMode, ConcurrentMode} from './ReactTypeOfMode';
106101
import {
107102
HostRoot,
108103
IndeterminateComponent,
@@ -2860,34 +2855,6 @@ function shouldForceFlushFallbacksInDEV() {
28602855
return __DEV__ && ReactCurrentActQueue.current !== null;
28612856
}
28622857

2863-
export function warnIfNotCurrentlyActingEffectsInDEV(fiber: Fiber): void {
2864-
if (__DEV__) {
2865-
const isActEnvironment =
2866-
fiber.mode & ConcurrentMode
2867-
? isConcurrentActEnvironment()
2868-
: isLegacyActEnvironment(fiber);
2869-
if (
2870-
isActEnvironment &&
2871-
(fiber.mode & StrictLegacyMode) !== NoMode &&
2872-
ReactCurrentActQueue.current === null
2873-
) {
2874-
console.error(
2875-
'An update to %s ran an effect, but was not wrapped in act(...).\n\n' +
2876-
'When testing, code that causes React state updates should be ' +
2877-
'wrapped into act(...):\n\n' +
2878-
'act(() => {\n' +
2879-
' /* fire events that update state */\n' +
2880-
'});\n' +
2881-
'/* assert on the output */\n\n' +
2882-
"This ensures that you're testing the behavior the user would see " +
2883-
'in the browser.' +
2884-
' Learn more at https://reactjs.org/link/wrap-tests-with-act',
2885-
getComponentNameFromFiber(fiber),
2886-
);
2887-
}
2888-
}
2889-
}
2890-
28912858
function warnIfUpdatesNotWrappedWithActDEV(fiber: Fiber): void {
28922859
if (__DEV__) {
28932860
if (fiber.mode & ConcurrentMode) {

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

+1-34
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,7 @@ import {
9797
createWorkInProgress,
9898
assignFiberPropertiesInDEV,
9999
} from './ReactFiber.old';
100-
import {
101-
NoMode,
102-
StrictLegacyMode,
103-
ProfileMode,
104-
ConcurrentMode,
105-
} from './ReactTypeOfMode';
100+
import {NoMode, ProfileMode, ConcurrentMode} from './ReactTypeOfMode';
106101
import {
107102
HostRoot,
108103
IndeterminateComponent,
@@ -2860,34 +2855,6 @@ function shouldForceFlushFallbacksInDEV() {
28602855
return __DEV__ && ReactCurrentActQueue.current !== null;
28612856
}
28622857

2863-
export function warnIfNotCurrentlyActingEffectsInDEV(fiber: Fiber): void {
2864-
if (__DEV__) {
2865-
const isActEnvironment =
2866-
fiber.mode & ConcurrentMode
2867-
? isConcurrentActEnvironment()
2868-
: isLegacyActEnvironment(fiber);
2869-
if (
2870-
isActEnvironment &&
2871-
(fiber.mode & StrictLegacyMode) !== NoMode &&
2872-
ReactCurrentActQueue.current === null
2873-
) {
2874-
console.error(
2875-
'An update to %s ran an effect, but was not wrapped in act(...).\n\n' +
2876-
'When testing, code that causes React state updates should be ' +
2877-
'wrapped into act(...):\n\n' +
2878-
'act(() => {\n' +
2879-
' /* fire events that update state */\n' +
2880-
'});\n' +
2881-
'/* assert on the output */\n\n' +
2882-
"This ensures that you're testing the behavior the user would see " +
2883-
'in the browser.' +
2884-
' Learn more at https://reactjs.org/link/wrap-tests-with-act',
2885-
getComponentNameFromFiber(fiber),
2886-
);
2887-
}
2888-
}
2889-
}
2890-
28912858
function warnIfUpdatesNotWrappedWithActDEV(fiber: Fiber): void {
28922859
if (__DEV__) {
28932860
if (fiber.mode & ConcurrentMode) {

0 commit comments

Comments
 (0)