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

Prevent infinite re-renders in StrictMode + Offscreen #25203

Merged
merged 4 commits into from
Sep 9, 2022
Merged
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
9 changes: 8 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import {
LayoutMask,
PassiveMask,
PlacementDEV,
Visibility,
} from './ReactFiberFlags';
import {
NoLanes,
Expand Down Expand Up @@ -3184,9 +3185,15 @@ function doubleInvokeEffectsInDEV(
) {
const isStrictModeFiber = fiber.type === REACT_STRICT_MODE_TYPE;
const isInStrictMode = parentIsInStrictMode || isStrictModeFiber;

if (fiber.flags & PlacementDEV || fiber.tag === OffscreenComponent) {
setCurrentDebugFiberInDEV(fiber);
if (isInStrictMode) {
const isNotOffscreen = fiber.tag !== OffscreenComponent;
// Checks if Offscreen is being revealed. For all other components, evaluates to true.
const hasOffscreenBecomeVisible =
isNotOffscreen ||
(fiber.flags & Visibility && fiber.memoizedState === null);
if (isInStrictMode && hasOffscreenBecomeVisible) {
disappearLayoutEffects(fiber);
disconnectPassiveEffect(fiber);
reappearLayoutEffects(root, fiber.alternate, fiber, false);
Expand Down
9 changes: 8 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import {
LayoutMask,
PassiveMask,
PlacementDEV,
Visibility,
} from './ReactFiberFlags';
import {
NoLanes,
Expand Down Expand Up @@ -3184,9 +3185,15 @@ function doubleInvokeEffectsInDEV(
) {
const isStrictModeFiber = fiber.type === REACT_STRICT_MODE_TYPE;
const isInStrictMode = parentIsInStrictMode || isStrictModeFiber;

if (fiber.flags & PlacementDEV || fiber.tag === OffscreenComponent) {
setCurrentDebugFiberInDEV(fiber);
if (isInStrictMode) {
const isNotOffscreen = fiber.tag !== OffscreenComponent;
// Checks if Offscreen is being revealed. For all other components, evaluates to true.
const hasOffscreenBecomeVisible =
isNotOffscreen ||
(fiber.flags & Visibility && fiber.memoizedState === null);
if (isInStrictMode && hasOffscreenBecomeVisible) {
acdlite marked this conversation as resolved.
Show resolved Hide resolved
disappearLayoutEffects(fiber);
disconnectPassiveEffect(fiber);
reappearLayoutEffects(root, fiber.alternate, fiber, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ describe('ReactOffscreenStrictMode', () => {

log = [];

act(() => {
ReactNoop.render(
<React.StrictMode>
<Offscreen mode="hidden">
<Component label="A" />
<Component label="B" />
</Offscreen>
</React.StrictMode>,
);
});

expect(log).toEqual(['A: render', 'A: render', 'B: render', 'B: render']);

log = [];

act(() => {
ReactNoop.render(
<React.StrictMode>
Expand All @@ -91,5 +106,51 @@ describe('ReactOffscreenStrictMode', () => {
'A: useLayoutEffect mount',
'A: useEffect mount',
]);

log = [];

act(() => {
ReactNoop.render(
<React.StrictMode>
<Offscreen mode="hidden">
<Component label="A" />
</Offscreen>
</React.StrictMode>,
);
});

expect(log).toEqual([
'A: useLayoutEffect unmount',
'A: useEffect unmount',
'A: render',
'A: render',
]);
});

it('should not cause infinite render loop when StrictMode is used with Suspense and synchronous set states', () => {
// This is a regression test, see https://github.com/facebook/react/pull/25179 for more details.
function App() {
const [state, setState] = React.useState(false);

React.useLayoutEffect(() => {
setState(true);
}, []);

React.useEffect(() => {
// Empty useEffect with empty dependency array is needed to trigger infinite render loop.
}, []);

return state;
}

act(() => {
ReactNoop.render(
<React.StrictMode>
<React.Suspense>
<App />
</React.Suspense>
</React.StrictMode>,
);
});
});
});