Skip to content

Commit 3497ccc

Browse files
author
Brian Vaughn
authored
Add guard to handle modified React elements with non-string keys (#17164)
1 parent 0f64703 commit 3497ccc

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

packages/react-devtools-shared/src/__tests__/__snapshots__/store-test.js.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,3 +607,8 @@ exports[`Store should properly handle a root with no visible nodes: 1: mount 1`]
607607
`;
608608

609609
exports[`Store should properly handle a root with no visible nodes: 2: add host nodes 1`] = `[root]`;
610+
611+
exports[`Store should properly serialize non-string key values: 1: mount 1`] = `
612+
[root]
613+
<Child key="123">
614+
`;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,4 +837,15 @@ describe('Store', () => {
837837
act(() => ReactDOM.unmountComponentAtNode(containerB));
838838
expect(store.supportsProfiling).toBe(false);
839839
});
840+
841+
it('should properly serialize non-string key values', () => {
842+
const Child = () => null;
843+
844+
// Bypass React element's automatic stringifying of keys intentionally.
845+
// This is pretty hacky.
846+
const fauxElement = Object.assign({}, <Child />, {key: 123});
847+
848+
act(() => ReactDOM.render([fauxElement], document.createElement('div')));
849+
expect(store).toMatchSnapshot('1: mount');
850+
});
840851
});

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,12 @@ export function attach(
11571157
: 0;
11581158

11591159
let displayNameStringID = getStringID(displayName);
1160-
let keyStringID = getStringID(key);
1160+
1161+
// This check is a guard to handle a React element that has been modified
1162+
// in such a way as to bypass the default stringification of the "key" property.
1163+
let keyString = key === null ? null : '' + key;
1164+
let keyStringID = getStringID(keyString);
1165+
11611166
pushOperation(TREE_OPERATION_ADD);
11621167
pushOperation(id);
11631168
pushOperation(elementType);

0 commit comments

Comments
 (0)