Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -752,4 +752,97 @@ describe('ProfilingCache', () => {
utils.act(() => store.profilerStore.stopProfiling());
expect(container.textContent).toBe('About');
});

it('components that were deleted and added to updaters during the layout phase should not crash', () => {
let setChildUnmounted;
function Child() {
const [, setState] = React.useState(false);

React.useLayoutEffect(() => {
return () => setState(true);
});

return null;
}

function App() {
const [childUnmounted, _setChildUnmounted] = React.useState(false);
setChildUnmounted = _setChildUnmounted;
return <>{!childUnmounted && <Child />}</>;
}

const root = ReactDOM.createRoot(document.createElement('div'));
utils.act(() => root.render(<App />));
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => setChildUnmounted(true));
utils.act(() => store.profilerStore.stopProfiling());

const updaters = store.profilerStore.getCommitData(store.roots[0], 0)
.updaters;
expect(updaters.length).toEqual(1);
expect(updaters[0].displayName).toEqual('App');
});

it('components in a deleted subtree and added to updaters during the layout phase should not crash', () => {
let setChildUnmounted;
function Child() {
return <GrandChild />;
}

function GrandChild() {
const [, setState] = React.useState(false);

React.useLayoutEffect(() => {
return () => setState(true);
});

return null;
}

function App() {
const [childUnmounted, _setChildUnmounted] = React.useState(false);
setChildUnmounted = _setChildUnmounted;
return <>{!childUnmounted && <Child />}</>;
}

const root = ReactDOM.createRoot(document.createElement('div'));
utils.act(() => root.render(<App />));
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => setChildUnmounted(true));
utils.act(() => store.profilerStore.stopProfiling());

const updaters = store.profilerStore.getCommitData(store.roots[0], 0)
.updaters;
expect(updaters.length).toEqual(1);
expect(updaters[0].displayName).toEqual('App');
});

it('components that were deleted should not be added to updaters during the passive phase', () => {
let setChildUnmounted;
function Child() {
const [, setState] = React.useState(false);
React.useEffect(() => {
return () => setState(true);
});

return null;
}

function App() {
const [childUnmounted, _setChildUnmounted] = React.useState(false);
setChildUnmounted = _setChildUnmounted;
return <>{!childUnmounted && <Child />}</>;
}

const root = ReactDOM.createRoot(document.createElement('div'));
utils.act(() => root.render(<App />));
utils.act(() => store.profilerStore.startProfiling());
utils.act(() => setChildUnmounted(true));
utils.act(() => store.profilerStore.stopProfiling());

const updaters = store.profilerStore.getCommitData(store.roots[0], 0)
.updaters;
expect(updaters.length).toEqual(1);
expect(updaters[0].displayName).toEqual('App');
});
});
4 changes: 3 additions & 1 deletion packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2573,7 +2573,9 @@ export function attach(

function getUpdatersList(root): Array<SerializedElement> | null {
return root.memoizedUpdaters != null
? Array.from(root.memoizedUpdaters).map(fiberToSerializedElement)
? Array.from(root.memoizedUpdaters)
.filter(fiber => getFiberIDUnsafe(fiber) !== null)
.map(fiberToSerializedElement)
Copy link
Contributor

Choose a reason for hiding this comment

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

This change should fix this bug in DevTools for older versions of React too 👍🏼 Nice.

: null;
}

Expand Down