Skip to content

Commit b39d0ce

Browse files
eps1lonzhengjitf
authored andcommitted
DevTools: update error indices when elements are added/removed (facebook#22144)
1 parent 3441bb4 commit b39d0ce

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

packages/react-devtools-shared/src/__tests__/treeContext-test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,64 @@ describe('TreeListContext', () => {
21192119
`);
21202120
});
21212121

2122+
it('should update correctly when elements are added/removed', () => {
2123+
const container = document.createElement('div');
2124+
let errored = false;
2125+
function ErrorOnce() {
2126+
if (!errored) {
2127+
errored = true;
2128+
console.error('test-only:one-time-error');
2129+
}
2130+
return null;
2131+
}
2132+
withErrorsOrWarningsIgnored(['test-only:'], () =>
2133+
utils.act(() =>
2134+
legacyRender(
2135+
<React.Fragment>
2136+
<ErrorOnce key="error" />
2137+
</React.Fragment>,
2138+
container,
2139+
),
2140+
),
2141+
);
2142+
2143+
let renderer;
2144+
utils.act(() => (renderer = TestRenderer.create(<Contexts />)));
2145+
expect(state).toMatchInlineSnapshot(`
2146+
✕ 1, ⚠ 0
2147+
[root]
2148+
<ErrorOnce key="error"> ✕
2149+
`);
2150+
2151+
withErrorsOrWarningsIgnored(['test-only:'], () =>
2152+
utils.act(() =>
2153+
legacyRender(
2154+
<React.Fragment>
2155+
<Child />
2156+
<ErrorOnce key="error" />
2157+
</React.Fragment>,
2158+
container,
2159+
),
2160+
),
2161+
);
2162+
2163+
utils.act(() => renderer.update(<Contexts />));
2164+
expect(state).toMatchInlineSnapshot(`
2165+
✕ 1, ⚠ 0
2166+
[root]
2167+
<Child>
2168+
<ErrorOnce key="error"> ✕
2169+
`);
2170+
2171+
selectNextErrorOrWarning();
2172+
expect(state).toMatchInlineSnapshot(`
2173+
✕ 1, ⚠ 0
2174+
[root]
2175+
<Child>
2176+
→ <ErrorOnce key="error"> ✕
2177+
`);
2178+
});
2179+
21222180
it('should update select and auto-expand parts components within hidden parts of the tree', () => {
21232181
const Wrapper = ({children}) => children;
21242182

packages/react-devtools-shared/src/devtools/store.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,18 @@ export default class Store extends EventEmitter<{|
11871187
console.groupEnd();
11881188
}
11891189

1190+
const indicesOfCachedErrorsOrWarningsAreStale =
1191+
!haveErrorsOrWarningsChanged &&
1192+
(addedElementIDs.length > 0 || removedElementIDs.size > 0);
1193+
if (indicesOfCachedErrorsOrWarningsAreStale) {
1194+
this._cachedErrorAndWarningTuples.forEach(entry => {
1195+
const index = this.getIndexOfElementID(entry.id);
1196+
if (index !== null) {
1197+
entry.index = index;
1198+
}
1199+
});
1200+
}
1201+
11901202
this.emit('mutated', [addedElementIDs, removedElementIDs]);
11911203
};
11921204

0 commit comments

Comments
 (0)