Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress unmounted setState warning after flushing passive effects #15122

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 27 additions & 10 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Expand Up @@ -181,6 +181,7 @@ const {ReactCurrentDispatcher, ReactCurrentOwner} = ReactSharedInternals;

let didWarnAboutStateTransition;
let didWarnSetStateChildContext;
let suppressUpdateOnUnmountedWarning;
let warnAboutUpdateOnUnmounted;
let warnAboutInvalidUpdates;

Expand All @@ -199,6 +200,7 @@ if (enableSchedulerTracing) {
if (__DEV__) {
didWarnAboutStateTransition = false;
didWarnSetStateChildContext = false;
suppressUpdateOnUnmountedWarning = false;
const didWarnStateUpdateForUnmountedComponent = {};

warnAboutUpdateOnUnmounted = function(fiber: Fiber, isClass: boolean) {
Expand Down Expand Up @@ -605,6 +607,12 @@ function flushPassiveEffects() {
// We call the scheduled callback instead of commitPassiveEffects directly
// to ensure tracing works correctly.
passiveEffectCallback();
if (__DEV__) {
// Flushing passive effects could have led to unmounting a component
// that the user has already called setState on. So temporarily suppress
// any warnings about that until the next scheduleWork call.
suppressUpdateOnUnmountedWarning = true;
}
}
}

Expand Down Expand Up @@ -1847,18 +1855,27 @@ export function warnIfNotCurrentlyBatchingInDev(fiber: Fiber): void {

function scheduleWork(fiber: Fiber, expirationTime: ExpirationTime) {
const root = scheduleWorkToRoot(fiber, expirationTime);

let suppressWarning;
if (__DEV__) {
suppressWarning = suppressUpdateOnUnmountedWarning;
suppressUpdateOnUnmountedWarning = false;
}

if (root === null) {
if (__DEV__) {
switch (fiber.tag) {
case ClassComponent:
warnAboutUpdateOnUnmounted(fiber, true);
break;
case FunctionComponent:
case ForwardRef:
case MemoComponent:
case SimpleMemoComponent:
warnAboutUpdateOnUnmounted(fiber, false);
break;
if (!suppressWarning) {
switch (fiber.tag) {
case ClassComponent:
warnAboutUpdateOnUnmounted(fiber, true);
break;
case FunctionComponent:
case ForwardRef:
case MemoComponent:
case SimpleMemoComponent:
warnAboutUpdateOnUnmounted(fiber, false);
break;
}
}
}
return;
Expand Down
Expand Up @@ -1699,6 +1699,74 @@ describe('ReactHooks', () => {
).toThrow('Hello');
});

it('does not fire a false positive warning when previous effect unmounts the component', () => {
let {useState, useEffect} = React;
let globalListener;
let _setState;

function A() {
const [show, setShow] = useState(true);
function hideMe() {
setShow(false);
}
return show ? <B hideMe={hideMe} /> : null;
}

function B(props) {
return <C {...props} />;
}

function C({hideMe}) {
const [, setState] = useState();

useEffect(() => {
let isStale = false;

_setState = setState;
globalListener = () => {
if (!isStale) {
setState('hello');
}
if (!isStale) {
setState('goodbye');
}
if (!isStale) {
setState('hello');
}
};

return () => {
isStale = true;
hideMe();
};
});
return null;
}

ReactTestRenderer.act(() => {
ReactTestRenderer.create(<A />);
});

expect(() => {
globalListener();
globalListener();
}).toWarnDev([
'An update to C inside a test was not wrapped in act',
'An update to C inside a test was not wrapped in act',
// Note: should *not* warn about updates on unmounted component.
// Because there's no way for component to know it got unmounted.
]);

// Verify the warning isn't disabled permanently.
expect(() => {
ReactTestRenderer.act(() => {
_setState('bad');
});
}).toWarnDev([
"Can't perform a React state update on an unmounted component",
]);
});

// Regression test for https://github.com/facebook/react/issues/14790
it('does not fire a false positive warning when suspending memo', async () => {
const {Suspense, useState} = React;
Expand Down