Skip to content

Commit

Permalink
Nits
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Nov 16, 2022
1 parent 2736161 commit 9e135dc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
13 changes: 6 additions & 7 deletions packages/react/src/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,21 +382,20 @@ export function jsxWithValidation(
if (warnAboutSpreadingKeyToJSX) {
if (hasOwnProperty.call(props, 'key')) {
const componentName = getComponentNameFromType(type);
const keys = Object.keys(props);
const beforeExample = '{' + keys.join(': ..., ') + ': ...}';
const keys = Object.keys(props).filter(k => k !== 'key');
const beforeExample = keys.length > 0 ? '{key: someKey, ' + keys.join(': ..., ') + ': ...}' : '{key: someKey}';
if (!didWarnAboutKeySpread[componentName + beforeExample]) {
const keysWithoutKey = keys.filter(k => k !== 'key');
const afterExample =
keysWithoutKey.length > 0
? '{' + keysWithoutKey.join(': ..., ') + ': ...}'
keys.length > 0
? '{' + keys.join(': ..., ') + ': ...}'
: '{}';
console.error(
'An props object containing a "key" prop is being spread into JSX:\n' +
'A props object containing a "key" prop is being spread into JSX:\n' +
' let props = %s;\n' +
' <%s {...props} />\n' +
'React keys must be passed directly to JSX without using spread:\n' +
' let props = %s;\n' +
' <%s key={...} {...props} />',
' <%s key={someKey} {...props} />',
beforeExample,
componentName,
afterExample,
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/__tests__/ReactElementJSX-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,19 @@ describe('ReactElement.jsx', () => {
class Parent extends React.Component {
render() {
return JSXRuntime.jsx('div', {
children: [JSXRuntime.jsx(Child, {key: '0'})],
children: [JSXRuntime.jsx(Child, {key: '0', prop: 'hi'})],
});
}
}
expect(() =>
ReactDOM.render(JSXRuntime.jsx(Parent, {}), container),
).toErrorDev(
'Warning: An props object containing a "key" prop is being spread into JSX:\n' +
' let props = {key: ...};\n' +
'Warning: A props object containing a "key" prop is being spread into JSX:\n' +
' let props = {key: someKey, prop: ...};\n' +
' <Child {...props} />\n' +
'React keys must be passed directly to JSX without using spread:\n' +
' let props = {};\n' +
' <Child key={...} {...props} />',
' let props = {prop: ...};\n' +
' <Child key={someKey} {...props} />',
);
});
}
Expand Down
13 changes: 6 additions & 7 deletions packages/react/src/jsx/ReactJSXElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,21 +393,20 @@ export function jsxWithValidation(
if (warnAboutSpreadingKeyToJSX) {
if (hasOwnProperty.call(props, 'key')) {
const componentName = getComponentNameFromType(type);
const keys = Object.keys(props);
const beforeExample = '{' + keys.join(': ..., ') + ': ...}';
const keys = Object.keys(props).filter(k => k !== 'key');
const beforeExample = keys.length > 0 ? '{key: someKey, ' + keys.join(': ..., ') + ': ...}' : '{key: someKey}';
if (!didWarnAboutKeySpread[componentName + beforeExample]) {
const keysWithoutKey = keys.filter(k => k !== 'key');
const afterExample =
keysWithoutKey.length > 0
? '{' + keysWithoutKey.join(': ..., ') + ': ...}'
keys.length > 0
? '{' + keys.join(': ..., ') + ': ...}'
: '{}';
console.error(
'An props object containing a "key" prop is being spread into JSX:\n' +
'A props object containing a "key" prop is being spread into JSX:\n' +
' let props = %s;\n' +
' <%s {...props} />\n' +
'React keys must be passed directly to JSX without using spread:\n' +
' let props = %s;\n' +
' <%s key={...} {...props} />',
' <%s key={someKey} {...props} />',
beforeExample,
componentName,
afterExample,
Expand Down

0 comments on commit 9e135dc

Please sign in to comment.