Skip to content

Commit 4c18085

Browse files
committed
[DevTools] Display React.optimisticKey in key positions
1 parent bd517c6 commit 4c18085

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

packages/react-devtools-shared/src/__tests__/optimisticKeyDevToolsIntegration.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('Store React.optimisticKey', () => {
8989

9090
expect(store).toMatchInlineSnapshot(`
9191
[root]
92-
<Component key="Symbol(react.optimistic_key)">
92+
<Component key="React.optimisticKey">
9393
`);
9494
expect(store.getElementAtIndex(0)).toEqual(
9595
expect.objectContaining({key: 'React.optimisticKey'}),
@@ -109,23 +109,23 @@ describe('Store React.optimisticKey', () => {
109109

110110
expect(state).toMatchInlineSnapshot(`
111111
[root]
112-
<Fragment key="Symbol(react.optimistic_key)">
112+
<Fragment key="React.optimisticKey">
113113
`);
114114

115115
act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'optimistic'}));
116116
act(() => renderer.update(<Contexts />));
117117

118118
expect(state).toMatchInlineSnapshot(`
119119
[root]
120-
<Fragment key="Symbol(react.optimistic_key)">
120+
<Fragment key="React.optimisticKey">
121121
`);
122122

123-
act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'symbol'}));
123+
act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'react'}));
124124
act(() => renderer.update(<Contexts />));
125125

126126
expect(state).toMatchInlineSnapshot(`
127127
[root]
128-
→ <Fragment key="Symbol(react.optimistic_key)">
128+
→ <Fragment key="React.optimisticKey">
129129
`);
130130
});
131131
});

packages/react-devtools-shared/src/backend/fiber/renderer.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,7 +2606,12 @@ export function attach(
26062606
26072607
// This check is a guard to handle a React element that has been modified
26082608
// in such a way as to bypass the default stringification of the "key" property.
2609-
const keyString = key === null ? null : String(key);
2609+
const keyString =
2610+
key === null
2611+
? null
2612+
: key === REACT_OPTIMISTIC_KEY
2613+
? 'React.optimisticKey'
2614+
: String(key);
26102615
const keyStringID = getStringID(keyString);
26112616
26122617
const nameProp =
@@ -6179,7 +6184,10 @@ export function attach(
61796184
return {
61806185
displayName: getDisplayNameForFiber(fiber) || 'Anonymous',
61816186
id: instance.id,
6182-
key: fiber.key === REACT_OPTIMISTIC_KEY ? null : fiber.key,
6187+
key:
6188+
fiber.key === REACT_OPTIMISTIC_KEY
6189+
? 'React.optimisticKey'
6190+
: fiber.key,
61836191
env: null,
61846192
stack:
61856193
fiber._debugOwner == null || fiber._debugStack == null
@@ -6195,7 +6203,7 @@ export function attach(
61956203
key:
61966204
componentInfo.key == null ||
61976205
componentInfo.key === REACT_OPTIMISTIC_KEY
6198-
? null
6206+
? 'React.optimisticKey'
61996207
: componentInfo.key,
62006208
env: componentInfo.env == null ? null : componentInfo.env,
62016209
stack:
@@ -7120,7 +7128,12 @@ export function attach(
71207128
// Does the component have legacy context attached to it.
71217129
hasLegacyContext,
71227130
7123-
key: key != null && key !== REACT_OPTIMISTIC_KEY ? key : null,
7131+
key:
7132+
key != null
7133+
? key === REACT_OPTIMISTIC_KEY
7134+
? 'React.optimisticKey'
7135+
: key
7136+
: null,
71247137
71257138
type: elementType,
71267139

0 commit comments

Comments
 (0)