Skip to content

Don't double-invoke effects in legacy roots #20028

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

Merged
merged 1 commit into from
Oct 15, 2020
Merged
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
33 changes: 28 additions & 5 deletions packages/react-reconciler/src/ReactFiberClassComponent.new.js
Original file line number Diff line number Diff line change
@@ -30,7 +30,13 @@ import invariant from 'shared/invariant';
import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols';

import {resolveDefaultProps} from './ReactFiberLazyComponent.new';
import {DebugTracingMode, StrictMode} from './ReactTypeOfMode';
import {
BlockingMode,
ConcurrentMode,
DebugTracingMode,
NoMode,
StrictMode,
} from './ReactTypeOfMode';

import {
enqueueUpdate,
@@ -891,7 +897,12 @@ function mountClassInstance(
}

if (typeof instance.componentDidMount === 'function') {
if (__DEV__ && enableDoubleInvokingEffects) {
if (
__DEV__ &&
enableDoubleInvokingEffects &&
(workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode
) {
// Never double-invoke effects for legacy roots.
workInProgress.flags |= MountLayoutDev | Update;
} else {
workInProgress.flags |= Update;
@@ -965,7 +976,11 @@ function resumeMountClassInstance(
// If an update was already in progress, we should schedule an Update
// effect even though we're bailing out, so that cWU/cDU are called.
if (typeof instance.componentDidMount === 'function') {
if (__DEV__ && enableDoubleInvokingEffects) {
if (
__DEV__ &&
enableDoubleInvokingEffects &&
(workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode
) {
workInProgress.flags |= MountLayoutDev | Update;
} else {
workInProgress.flags |= Update;
@@ -1012,7 +1027,11 @@ function resumeMountClassInstance(
}
}
if (typeof instance.componentDidMount === 'function') {
if (__DEV__ && enableDoubleInvokingEffects) {
if (
__DEV__ &&
enableDoubleInvokingEffects &&
(workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode
) {
workInProgress.flags |= MountLayoutDev | Update;
} else {
workInProgress.flags |= Update;
@@ -1022,7 +1041,11 @@ function resumeMountClassInstance(
// If an update was already in progress, we should schedule an Update
// effect even though we're bailing out, so that cWU/cDU are called.
if (typeof instance.componentDidMount === 'function') {
if (__DEV__ && enableDoubleInvokingEffects) {
if (
__DEV__ &&
enableDoubleInvokingEffects &&
(workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode
) {
workInProgress.flags |= MountLayoutDev | Update;
} else {
workInProgress.flags |= Update;
8 changes: 8 additions & 0 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
@@ -2024,6 +2024,8 @@ function commitPassiveMount(

function invokeLayoutEffectMountInDEV(fiber: Fiber): void {
if (__DEV__ && enableDoubleInvokingEffects) {
// We don't need to re-check for legacy roots here.
// This function will not be called within legacy roots.
switch (fiber.tag) {
case FunctionComponent:
case ForwardRef:
@@ -2057,6 +2059,8 @@ function invokeLayoutEffectMountInDEV(fiber: Fiber): void {

function invokePassiveEffectMountInDEV(fiber: Fiber): void {
if (__DEV__ && enableDoubleInvokingEffects) {
// We don't need to re-check for legacy roots here.
// This function will not be called within legacy roots.
switch (fiber.tag) {
case FunctionComponent:
case ForwardRef:
@@ -2081,6 +2085,8 @@ function invokePassiveEffectMountInDEV(fiber: Fiber): void {

function invokeLayoutEffectUnmountInDEV(fiber: Fiber): void {
if (__DEV__ && enableDoubleInvokingEffects) {
// We don't need to re-check for legacy roots here.
// This function will not be called within legacy roots.
switch (fiber.tag) {
case FunctionComponent:
case ForwardRef:
@@ -2113,6 +2119,8 @@ function invokeLayoutEffectUnmountInDEV(fiber: Fiber): void {

function invokePassiveEffectUnmountInDEV(fiber: Fiber): void {
if (__DEV__ && enableDoubleInvokingEffects) {
// We don't need to re-check for legacy roots here.
// This function will not be called within legacy roots.
switch (fiber.tag) {
case FunctionComponent:
case ForwardRef:
31 changes: 26 additions & 5 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
@@ -29,7 +29,12 @@ import {
enableDoubleInvokingEffects,
} from 'shared/ReactFeatureFlags';

import {NoMode, BlockingMode, DebugTracingMode} from './ReactTypeOfMode';
import {
NoMode,
BlockingMode,
ConcurrentMode,
DebugTracingMode,
} from './ReactTypeOfMode';
import {
NoLane,
NoLanes,
@@ -485,7 +490,11 @@ export function bailoutHooks(
lanes: Lanes,
) {
workInProgress.updateQueue = current.updateQueue;
if (__DEV__ && enableDoubleInvokingEffects) {
if (
__DEV__ &&
enableDoubleInvokingEffects &&
(workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode
) {
workInProgress.flags &= ~(
MountPassiveDevEffect |
PassiveEffect |
@@ -1253,7 +1262,11 @@ function mountEffect(
}
}

if (__DEV__ && enableDoubleInvokingEffects) {
if (
__DEV__ &&
enableDoubleInvokingEffects &&
(currentlyRenderingFiber.mode & (BlockingMode | ConcurrentMode)) !== NoMode
) {
return mountEffectImpl(
MountPassiveDevEffect | PassiveEffect | PassiveStaticEffect,
HookPassive,
@@ -1287,7 +1300,11 @@ function mountLayoutEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
): void {
if (__DEV__ && enableDoubleInvokingEffects) {
if (
__DEV__ &&
enableDoubleInvokingEffects &&
(currentlyRenderingFiber.mode & (BlockingMode | ConcurrentMode)) !== NoMode
) {
return mountEffectImpl(
MountLayoutDevEffect | UpdateEffect,
HookLayout,
@@ -1355,7 +1372,11 @@ function mountImperativeHandle<T>(
const effectDeps =
deps !== null && deps !== undefined ? deps.concat([ref]) : null;

if (__DEV__ && enableDoubleInvokingEffects) {
if (
__DEV__ &&
enableDoubleInvokingEffects &&
(currentlyRenderingFiber.mode & (BlockingMode | ConcurrentMode)) !== NoMode
) {
return mountEffectImpl(
MountLayoutDevEffect | UpdateEffect,
HookLayout,
7 changes: 7 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
@@ -2874,6 +2874,11 @@ function commitDoubleInvokeEffectsInDEV(
hasPassiveEffects: boolean,
) {
if (__DEV__ && enableDoubleInvokingEffects) {
// Never double-invoke effects for legacy roots.
if ((fiber.mode & (BlockingMode | ConcurrentMode)) === NoMode) {
return;
}

setCurrentDebugFiberInDEV(fiber);
invokeEffectsInDev(fiber, MountLayoutDev, invokeLayoutEffectUnmountInDEV);
if (hasPassiveEffects) {
@@ -2898,6 +2903,8 @@ function invokeEffectsInDev(
invokeEffectFn: (fiber: Fiber) => void,
): void {
if (__DEV__ && enableDoubleInvokingEffects) {
// We don't need to re-check for legacy roots here.
// This function will not be called within legacy roots.
let fiber = firstChild;
while (fiber !== null) {
if (fiber.child !== null) {
Original file line number Diff line number Diff line change
@@ -11,17 +11,44 @@

let React;
let ReactFeatureFlags;
let ReactNoop;
let ReactTestRenderer;
let Scheduler;
let act;

describe('ReactDoubleInvokeEvents', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactNoop = require('react-noop-renderer');
ReactTestRenderer = require('react-test-renderer');
Scheduler = require('scheduler');
ReactFeatureFlags.enableDoubleInvokingEffects = __VARIANT__;
act = ReactTestRenderer.unstable_concurrentAct;
});

it('should not double invoke effects in legacy mode', () => {
function App({text}) {
React.useEffect(() => {
Scheduler.unstable_yieldValue('useEffect mount');
return () => Scheduler.unstable_yieldValue('useEffect unmount');
});

React.useLayoutEffect(() => {
Scheduler.unstable_yieldValue('useLayoutEffect mount');
return () => Scheduler.unstable_yieldValue('useLayoutEffect unmount');
});

return text;
}

act(() => {
ReactTestRenderer.create(<App text={'mount'} />);
});

expect(Scheduler).toHaveYielded([
'useLayoutEffect mount',
'useEffect mount',
]);
});

it('double invoking for effects works properly', () => {
@@ -38,8 +65,12 @@ describe('ReactDoubleInvokeEvents', () => {

return text;
}
ReactNoop.act(() => {
ReactNoop.render(<App text={'mount'} />);

let renderer;
act(() => {
renderer = ReactTestRenderer.create(<App text={'mount'} />, {
unstable_isConcurrent: true,
});
});

if (__DEV__ && __VARIANT__) {
@@ -58,8 +89,8 @@ describe('ReactDoubleInvokeEvents', () => {
]);
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'update'} />);
act(() => {
renderer.update(<App text={'update'} />);
});

expect(Scheduler).toHaveYielded([
@@ -69,8 +100,8 @@ describe('ReactDoubleInvokeEvents', () => {
'useEffect mount',
]);

ReactNoop.act(() => {
ReactNoop.render(null);
act(() => {
renderer.unmount();
});

expect(Scheduler).toHaveYielded([
@@ -94,8 +125,11 @@ describe('ReactDoubleInvokeEvents', () => {
return text;
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'mount'} />);
let renderer;
act(() => {
renderer = ReactTestRenderer.create(<App text={'mount'} />, {
unstable_isConcurrent: true,
});
});

if (__DEV__ && __VARIANT__) {
@@ -114,8 +148,8 @@ describe('ReactDoubleInvokeEvents', () => {
]);
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'update'} />);
act(() => {
renderer.update(<App text={'update'} />);
});

expect(Scheduler).toHaveYielded([
@@ -125,8 +159,8 @@ describe('ReactDoubleInvokeEvents', () => {
'useEffect Two mount',
]);

ReactNoop.act(() => {
ReactNoop.render(null);
act(() => {
renderer.unmount(null);
});

expect(Scheduler).toHaveYielded([
@@ -152,8 +186,11 @@ describe('ReactDoubleInvokeEvents', () => {
return text;
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'mount'} />);
let renderer;
act(() => {
renderer = ReactTestRenderer.create(<App text={'mount'} />, {
unstable_isConcurrent: true,
});
});

if (__DEV__ && __VARIANT__) {
@@ -172,8 +209,8 @@ describe('ReactDoubleInvokeEvents', () => {
]);
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'update'} />);
act(() => {
renderer.update(<App text={'update'} />);
});

expect(Scheduler).toHaveYielded([
@@ -183,8 +220,8 @@ describe('ReactDoubleInvokeEvents', () => {
'useLayoutEffect Two mount',
]);

ReactNoop.act(() => {
ReactNoop.render(null);
act(() => {
renderer.unmount();
});

expect(Scheduler).toHaveYielded([
@@ -206,8 +243,11 @@ describe('ReactDoubleInvokeEvents', () => {
return text;
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'mount'} />);
let renderer;
act(() => {
renderer = ReactTestRenderer.create(<App text={'mount'} />, {
unstable_isConcurrent: true,
});
});

if (__DEV__ && __VARIANT__) {
@@ -224,17 +264,17 @@ describe('ReactDoubleInvokeEvents', () => {
]);
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'update'} />);
act(() => {
renderer.update(<App text={'update'} />);
});

expect(Scheduler).toHaveYielded([
'useLayoutEffect mount',
'useEffect mount',
]);

ReactNoop.act(() => {
ReactNoop.render(null);
act(() => {
renderer.unmount();
});

expect(Scheduler).toHaveYielded([]);
@@ -264,8 +304,8 @@ describe('ReactDoubleInvokeEvents', () => {
}
}

ReactNoop.act(() => {
ReactNoop.render(<App />);
act(() => {
ReactTestRenderer.create(<App />, {unstable_isConcurrent: true});
});

if (__DEV__ && __VARIANT__) {
@@ -298,8 +338,11 @@ describe('ReactDoubleInvokeEvents', () => {
}
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'mount'} />);
let renderer;
act(() => {
renderer = ReactTestRenderer.create(<App text={'mount'} />, {
unstable_isConcurrent: true,
});
});

if (__DEV__ && __VARIANT__) {
@@ -312,19 +355,45 @@ describe('ReactDoubleInvokeEvents', () => {
expect(Scheduler).toHaveYielded(['componentDidMount']);
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'update'} />);
act(() => {
renderer.update(<App text={'update'} />);
});

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

ReactNoop.act(() => {
ReactNoop.render(null);
act(() => {
renderer.unmount();
Copy link
Member

@rickhanlonii rickhanlonii Oct 14, 2020

Choose a reason for hiding this comment

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

Just curious - why are you switching from render to update/unmount?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renderer.unmount() is the canonical way to unmount a root using the test renderer. renderer.update(null) would have also worked here.

Copy link
Member

Choose a reason for hiding this comment

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

So you were just cleaning up the .render calls?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. In order to test the changes made (legacy root vs modern root) I needed to use a renderer that supported both. ReactNoop did not, so I switched to react-test-renderer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine but React Noop does support legacy mode via createLegacySyncRoot

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah! I forgot about that method.

Copy link
Member

Choose a reason for hiding this comment

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

Ah got it, thanks for the context!

});

expect(Scheduler).toHaveYielded(['componentWillUnmount']);
});

it('should not double invoke class lifecycles in legacy mode', () => {
class App extends React.PureComponent {
componentDidMount() {
Scheduler.unstable_yieldValue('componentDidMount');
}

componentDidUpdate() {
Scheduler.unstable_yieldValue('componentDidUpdate');
}

componentWillUnmount() {
Scheduler.unstable_yieldValue('componentWillUnmount');
}

render() {
return this.props.text;
}
}

act(() => {
ReactTestRenderer.create(<App text={'mount'} />);
});

expect(Scheduler).toHaveYielded(['componentDidMount']);
});

it('double flushing passive effects only results in one double invoke', () => {
function App({text}) {
const [state, setState] = React.useState(0);
@@ -345,8 +414,10 @@ describe('ReactDoubleInvokeEvents', () => {
return text;
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'mount'} />);
act(() => {
ReactTestRenderer.create(<App text={'mount'} />, {
unstable_isConcurrent: true,
});
});

if (__DEV__ && __VARIANT__) {
@@ -410,8 +481,8 @@ describe('ReactDoubleInvokeEvents', () => {
return showChild && <Child />;
}

ReactNoop.act(() => {
ReactNoop.render(<App />);
act(() => {
ReactTestRenderer.create(<App />, {unstable_isConcurrent: true});
});

if (__DEV__ && __VARIANT__) {
@@ -430,7 +501,7 @@ describe('ReactDoubleInvokeEvents', () => {
]);
}

ReactNoop.act(() => {
act(() => {
_setShowChild(true);
});

@@ -495,8 +566,11 @@ describe('ReactDoubleInvokeEvents', () => {
);
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'mount'} />);
let renderer;
act(() => {
renderer = ReactTestRenderer.create(<App text={'mount'} />, {
unstable_isConcurrent: true,
});
});

if (__DEV__ && __VARIANT__) {
@@ -519,8 +593,8 @@ describe('ReactDoubleInvokeEvents', () => {
]);
}

ReactNoop.act(() => {
ReactNoop.render(<App text={'mount'} />);
act(() => {
renderer.update(<App text={'mount'} />);
});

expect(Scheduler).toHaveYielded([
@@ -530,8 +604,8 @@ describe('ReactDoubleInvokeEvents', () => {
'useEffect mount',
]);

ReactNoop.act(() => {
ReactNoop.render(null);
act(() => {
renderer.unmount();
});

expect(Scheduler).toHaveYielded([
6 changes: 5 additions & 1 deletion packages/react/src/__tests__/ReactProfiler-test.internal.js
Original file line number Diff line number Diff line change
@@ -4180,7 +4180,11 @@ describe('Profiler', () => {
const interactions = SchedulerTracing.unstable_getCurrent();
expect(interactions.size).toBe(1);
interaction = Array.from(interactions)[0];
ReactTestRenderer.create(<Component />);
ReactTestRendererAct(() => {
ReactTestRenderer.create(<Component />, {
unstable_isConcurrent: true,
});
});
},
);
Scheduler.unstable_flushAll();