Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guard to handle modified React elements with non-string keys #17164

Merged
merged 1 commit into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
7 changes: 6 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,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