Skip to content

Commit

Permalink
Add guard to handle modified React elements with non-string keys (#17164
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Brian Vaughn committed Oct 29, 2019
1 parent 0f64703 commit 3497ccc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Expand Up @@ -607,3 +607,8 @@ exports[`Store should properly handle a root with no visible nodes: 1: mount 1`]
`;

exports[`Store should properly handle a root with no visible nodes: 2: add host nodes 1`] = `[root]`;

exports[`Store should properly serialize non-string key values: 1: mount 1`] = `
[root]
<Child key="123">
`;
11 changes: 11 additions & 0 deletions packages/react-devtools-shared/src/__tests__/store-test.js
Expand Up @@ -837,4 +837,15 @@ describe('Store', () => {
act(() => ReactDOM.unmountComponentAtNode(containerB));
expect(store.supportsProfiling).toBe(false);
});

it('should properly serialize non-string key values', () => {
const Child = () => null;

// Bypass React element's automatic stringifying of keys intentionally.
// This is pretty hacky.
const fauxElement = Object.assign({}, <Child />, {key: 123});

act(() => ReactDOM.render([fauxElement], document.createElement('div')));
expect(store).toMatchSnapshot('1: mount');
});
});
7 changes: 6 additions & 1 deletion packages/react-devtools-shared/src/backend/renderer.js
Expand Up @@ -1157,7 +1157,12 @@ export function attach(
: 0;

let displayNameStringID = getStringID(displayName);
let keyStringID = getStringID(key);

// This check is a guard to handle a React element that has been modified
// in such a way as to bypass the default stringification of the "key" property.
let keyString = key === null ? null : '' + key;
let keyStringID = getStringID(keyString);

pushOperation(TREE_OPERATION_ADD);
pushOperation(id);
pushOperation(elementType);
Expand Down

0 comments on commit 3497ccc

Please sign in to comment.