Skip to content

Commit

Permalink
Deletion effects should fire parent -> child (#20584)
Browse files Browse the repository at this point in the history
* Test: Deletion effects should fire parent -> child

Regression in new effect implementation

* Fix passive deletion effect ordering
  • Loading branch information
acdlite authored Jan 14, 2021
1 parent fc07b07 commit a656ace
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
12 changes: 7 additions & 5 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,13 @@ function commitPassiveUnmountEffectsInsideOfDeletedTree_begin(
) {
while (nextEffect !== null) {
const fiber = nextEffect;

// Deletion effects fire in parent -> child order
// TODO: Check if fiber has a PassiveStatic flag
setCurrentDebugFiberInDEV(fiber);
commitPassiveUnmountInsideDeletedTreeOnFiber(fiber);
resetCurrentDebugFiberInDEV();

const child = fiber.child;
// TODO: Only traverse subtree if it has a PassiveStatic flag
if (child !== null) {
Expand All @@ -2023,11 +2030,6 @@ function commitPassiveUnmountEffectsInsideOfDeletedTree_complete(
) {
while (nextEffect !== null) {
const fiber = nextEffect;
// TODO: Check if fiber has a PassiveStatic flag
setCurrentDebugFiberInDEV(fiber);
commitPassiveUnmountInsideDeletedTreeOnFiber(fiber);
resetCurrentDebugFiberInDEV();

if (fiber === deletedSubtreeRoot) {
nextEffect = null;
return;
Expand Down
12 changes: 7 additions & 5 deletions packages/react-reconciler/src/ReactFiberCommitWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,13 @@ function commitPassiveUnmountEffectsInsideOfDeletedTree_begin(
) {
while (nextEffect !== null) {
const fiber = nextEffect;

// Deletion effects fire in parent -> child order
// TODO: Check if fiber has a PassiveStatic flag
setCurrentDebugFiberInDEV(fiber);
commitPassiveUnmountInsideDeletedTreeOnFiber(fiber);
resetCurrentDebugFiberInDEV();

const child = fiber.child;
// TODO: Only traverse subtree if it has a PassiveStatic flag
if (child !== null) {
Expand All @@ -2023,11 +2030,6 @@ function commitPassiveUnmountEffectsInsideOfDeletedTree_complete(
) {
while (nextEffect !== null) {
const fiber = nextEffect;
// TODO: Check if fiber has a PassiveStatic flag
setCurrentDebugFiberInDEV(fiber);
commitPassiveUnmountInsideDeletedTreeOnFiber(fiber);
resetCurrentDebugFiberInDEV();

if (fiber === deletedSubtreeRoot) {
nextEffect = null;
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
* @jest-environment node
*/

/* eslint-disable no-func-assign */

'use strict';

let React;
let ReactNoop;
let Scheduler;
let useEffect;
let useLayoutEffect;

describe('ReactHooksWithNoopRenderer', () => {
beforeEach(() => {
jest.resetModules();
jest.useFakeTimers();

React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
useEffect = React.useEffect;
useLayoutEffect = React.useLayoutEffect;
});

test('layout unmmouts on deletion are fired in parent -> child order', async () => {
const root = ReactNoop.createRoot();

function Parent() {
useLayoutEffect(() => {
return () => Scheduler.unstable_yieldValue('Unmount parent');
});
return <Child />;
}

function Child() {
useLayoutEffect(() => {
return () => Scheduler.unstable_yieldValue('Unmount child');
});
return 'Child';
}

await ReactNoop.act(async () => {
root.render(<Parent />);
});
expect(root).toMatchRenderedOutput('Child');
await ReactNoop.act(async () => {
root.render(null);
});
expect(Scheduler).toHaveYielded(['Unmount parent', 'Unmount child']);
});

test('passive unmmouts on deletion are fired in parent -> child order', async () => {
const root = ReactNoop.createRoot();

function Parent() {
useEffect(() => {
return () => Scheduler.unstable_yieldValue('Unmount parent');
});
return <Child />;
}

function Child() {
useEffect(() => {
return () => Scheduler.unstable_yieldValue('Unmount child');
});
return 'Child';
}

await ReactNoop.act(async () => {
root.render(<Parent />);
});
expect(root).toMatchRenderedOutput('Child');
await ReactNoop.act(async () => {
root.render(null);
});
expect(Scheduler).toHaveYielded(['Unmount parent', 'Unmount child']);
});
});

0 comments on commit a656ace

Please sign in to comment.