Skip to content

Commit 9e135dc

Browse files
committed
Nits
1 parent 2736161 commit 9e135dc

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

packages/react/src/ReactElementValidator.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,20 @@ export function jsxWithValidation(
382382
if (warnAboutSpreadingKeyToJSX) {
383383
if (hasOwnProperty.call(props, 'key')) {
384384
const componentName = getComponentNameFromType(type);
385-
const keys = Object.keys(props);
386-
const beforeExample = '{' + keys.join(': ..., ') + ': ...}';
385+
const keys = Object.keys(props).filter(k => k !== 'key');
386+
const beforeExample = keys.length > 0 ? '{key: someKey, ' + keys.join(': ..., ') + ': ...}' : '{key: someKey}';
387387
if (!didWarnAboutKeySpread[componentName + beforeExample]) {
388-
const keysWithoutKey = keys.filter(k => k !== 'key');
389388
const afterExample =
390-
keysWithoutKey.length > 0
391-
? '{' + keysWithoutKey.join(': ..., ') + ': ...}'
389+
keys.length > 0
390+
? '{' + keys.join(': ..., ') + ': ...}'
392391
: '{}';
393392
console.error(
394-
'An props object containing a "key" prop is being spread into JSX:\n' +
393+
'A props object containing a "key" prop is being spread into JSX:\n' +
395394
' let props = %s;\n' +
396395
' <%s {...props} />\n' +
397396
'React keys must be passed directly to JSX without using spread:\n' +
398397
' let props = %s;\n' +
399-
' <%s key={...} {...props} />',
398+
' <%s key={someKey} {...props} />',
400399
beforeExample,
401400
componentName,
402401
afterExample,

packages/react/src/__tests__/ReactElementJSX-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,19 +275,19 @@ describe('ReactElement.jsx', () => {
275275
class Parent extends React.Component {
276276
render() {
277277
return JSXRuntime.jsx('div', {
278-
children: [JSXRuntime.jsx(Child, {key: '0'})],
278+
children: [JSXRuntime.jsx(Child, {key: '0', prop: 'hi'})],
279279
});
280280
}
281281
}
282282
expect(() =>
283283
ReactDOM.render(JSXRuntime.jsx(Parent, {}), container),
284284
).toErrorDev(
285-
'Warning: An props object containing a "key" prop is being spread into JSX:\n' +
286-
' let props = {key: ...};\n' +
285+
'Warning: A props object containing a "key" prop is being spread into JSX:\n' +
286+
' let props = {key: someKey, prop: ...};\n' +
287287
' <Child {...props} />\n' +
288288
'React keys must be passed directly to JSX without using spread:\n' +
289-
' let props = {};\n' +
290-
' <Child key={...} {...props} />',
289+
' let props = {prop: ...};\n' +
290+
' <Child key={someKey} {...props} />',
291291
);
292292
});
293293
}

packages/react/src/jsx/ReactJSXElementValidator.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,21 +393,20 @@ export function jsxWithValidation(
393393
if (warnAboutSpreadingKeyToJSX) {
394394
if (hasOwnProperty.call(props, 'key')) {
395395
const componentName = getComponentNameFromType(type);
396-
const keys = Object.keys(props);
397-
const beforeExample = '{' + keys.join(': ..., ') + ': ...}';
396+
const keys = Object.keys(props).filter(k => k !== 'key');
397+
const beforeExample = keys.length > 0 ? '{key: someKey, ' + keys.join(': ..., ') + ': ...}' : '{key: someKey}';
398398
if (!didWarnAboutKeySpread[componentName + beforeExample]) {
399-
const keysWithoutKey = keys.filter(k => k !== 'key');
400399
const afterExample =
401-
keysWithoutKey.length > 0
402-
? '{' + keysWithoutKey.join(': ..., ') + ': ...}'
400+
keys.length > 0
401+
? '{' + keys.join(': ..., ') + ': ...}'
403402
: '{}';
404403
console.error(
405-
'An props object containing a "key" prop is being spread into JSX:\n' +
404+
'A props object containing a "key" prop is being spread into JSX:\n' +
406405
' let props = %s;\n' +
407406
' <%s {...props} />\n' +
408407
'React keys must be passed directly to JSX without using spread:\n' +
409408
' let props = %s;\n' +
410-
' <%s key={...} {...props} />',
409+
' <%s key={someKey} {...props} />',
411410
beforeExample,
412411
componentName,
413412
afterExample,

0 commit comments

Comments
 (0)