Skip to content

Commit

Permalink
Add guard to handle modified React elements with non-string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Oct 21, 2019
1 parent f7ec65e commit a9185a8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
});
});
10 changes: 9 additions & 1 deletion packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,15 @@ 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 = null;
if (key !== null) {
keyString = typeof key === 'string' ? key : '' + key;
}
let keyStringID = getStringID(keyString);

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

0 comments on commit a9185a8

Please sign in to comment.