Skip to content

fix(act): Don't warn if an effect was not queued #19319

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

Closed
wants to merge 7 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,30 @@ describe('ReactTestUtils.act()', () => {
'An update to App ran an effect, but was not wrapped in act(...)',
]);
});

it('does not warn on other effects if root is strict', () => {
function Component(props) {
React.useLayoutEffect(() => {
Scheduler.unstable_yieldValue('layout');
});
React.useImperativeHandle(React.createRef(), () => {
Scheduler.unstable_yieldValue('imperative handle');
});
return null;
}

expect(() => {
const root = ReactDOM.createRoot(document.createElement('div'), {
unstable_strictMode: true,
});
root.render(<Component />);
// confidence check that effects were mounted
expect(Scheduler).toFlushAndYield(['layout', 'imperative handle']);
root.render(<Component />);
// confidence check that effects were updated
expect(Scheduler).toFlushAndYield(['layout', 'imperative handle']);
}).toErrorDev([]);
});
});
});

Expand Down Expand Up @@ -353,6 +377,39 @@ function runActTests(label, render, unmount, rerender) {

expect(container.innerHTML).toBe('2');
});

// @gate __DEV__
it('does not warn on effects that were not queued', () => {
function Component() {
React.useEffect(() => {
Scheduler.unstable_yieldValue('effect');
}, []);
return null;
}

act(() => {
render(
<React.StrictMode>
<Component />
</React.StrictMode>,
container,
);
});

expect(Scheduler).toHaveYielded(['effect']);

expect(() => {
rerender(
<React.StrictMode>
<Component />
</React.StrictMode>,
container,
);
}).toErrorDev([]);

// Nothing scheduled so we should not have any unexpected errors regarding missing act();
expect(Scheduler).toFlushAndYield([]);
});
});
});

Expand Down
16 changes: 10 additions & 6 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
HasEffect as HookHasEffect,
Layout as HookLayout,
Passive as HookPassive,
NoFlags as HookNoFlags,
} from './ReactHookEffectTags';
import {
getWorkInProgressRoot,
Expand Down Expand Up @@ -1430,6 +1431,15 @@ function updateEffectImpl(fiberFlags, hookFlags, create, deps): void {
}
}

if (__DEV__) {
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
if ('undefined' !== typeof jest) {
if ((hookFlags & HookPassive) !== HookNoFlags) {
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
}
}
}

currentlyRenderingFiber.flags |= fiberFlags;

hook.memoizedState = pushEffect(
Expand Down Expand Up @@ -1475,12 +1485,6 @@ function updateEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
): void {
if (__DEV__) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this in mountEffect as well for symmetry?

// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
if ('undefined' !== typeof jest) {
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
}
}
return updateEffectImpl(PassiveEffect, HookPassive, create, deps);
}

Expand Down
16 changes: 10 additions & 6 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
HasEffect as HookHasEffect,
Layout as HookLayout,
Passive as HookPassive,
NoFlags as HookNoFlags,
} from './ReactHookEffectTags';
import {
getWorkInProgressRoot,
Expand Down Expand Up @@ -1430,6 +1431,15 @@ function updateEffectImpl(fiberFlags, hookFlags, create, deps): void {
}
}

if (__DEV__) {
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
if ('undefined' !== typeof jest) {
if ((hookFlags & HookPassive) !== HookNoFlags) {
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
}
}
}

currentlyRenderingFiber.flags |= fiberFlags;

hook.memoizedState = pushEffect(
Expand Down Expand Up @@ -1475,12 +1485,6 @@ function updateEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
): void {
if (__DEV__) {
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
if ('undefined' !== typeof jest) {
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
}
}
return updateEffectImpl(PassiveEffect, HookPassive, create, deps);
}

Expand Down