GeezerVison is showing github.com — Validate key in Flight · react/react@168b54e
File treeExpand file treeCollapse file tree Open diff view settings react-server-dom-webpack/src/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClient.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -565,6 +565,7 @@ function createElement( | 565 | 565 | props: mixed, | 566 | 566 | owner: null | ReactComponentInfo, // DEV-only | 567 | 567 | stack: null | string, // DEV-only | | 568 | + validated: number, // DEV-only | 568 | 569 | ): React$Element<any> { | 569 | 570 | let element: any; | 570 | 571 | if (__DEV__ && enableRefAsProp) { | @@ -616,7 +617,7 @@ function createElement( | 616 | 617 | configurable: false, | 617 | 618 | enumerable: false, | 618 | 619 | writable: true, | 619 | | - value: 1, // This element has already been validated on the server. | | 620 | + value: enableOwnerStacks ? validated : 1, // Whether the element has already been validated on the server. | 620 | 621 | }); | 621 | 622 | // debugInfo contains Server Component debug information. | 622 | 623 | Object.defineProperty(element, '_debugInfo', { | @@ -630,7 +631,7 @@ function createElement( | 630 | 631 | configurable: false, | 631 | 632 | enumerable: false, | 632 | 633 | writable: true, | 633 | | - value: {stack: stack}, | | 634 | + value: stack, | 634 | 635 | }); | 635 | 636 | Object.defineProperty(element, '_debugTask', { | 636 | 637 | configurable: false, | @@ -1039,6 +1040,7 @@ function parseModelTuple( | 1039 | 1040 | tuple[3], | 1040 | 1041 | __DEV__ ? (tuple: any)[4] : null, | 1041 | 1042 | __DEV__ && enableOwnerStacks ? (tuple: any)[5] : null, | | 1043 | + __DEV__ && enableOwnerStacks ? (tuple: any)[6] : 0, | 1042 | 1044 | ); | 1043 | 1045 | } | 1044 | 1046 | return value; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/__tests__/ReactFlight-test.js +29-8Lines changed: 29 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1389,19 +1389,40 @@ describe('ReactFlight', () => { | 1389 | 1389 | ReactNoopFlightClient.read(transport); | 1390 | 1390 | }); | 1391 | 1391 |
| 1392 | | - it('should warn in DEV a child is missing keys', () => { | | 1392 | + it('should warn in DEV a child is missing keys on server component', () => { | | 1393 | + function NoKey({children}) { | | 1394 | + return <div key="this has a key but parent doesn't" />; | | 1395 | + } | | 1396 | + expect(() => { | | 1397 | + const transport = ReactNoopFlightServer.render( | | 1398 | + <div>{Array(6).fill(<NoKey />)}</div>, | | 1399 | + ); | | 1400 | + ReactNoopFlightClient.read(transport); | | 1401 | + }).toErrorDev('Each child in a list should have a unique "key" prop.', { | | 1402 | + withoutStack: gate(flags => flags.enableOwnerStacks), | | 1403 | + }); | | 1404 | + }); | | 1405 | + | | 1406 | + it('should warn in DEV a child is missing keys in client component', async () => { | 1393 | 1407 | function ParentClient({children}) { | 1394 | 1408 | return children; | 1395 | 1409 | } | 1396 | 1410 | const Parent = clientReference(ParentClient); | 1397 | | - expect(() => { | | 1411 | + await expect(async () => { | 1398 | 1412 | const transport = ReactNoopFlightServer.render( | 1399 | 1413 | <Parent>{Array(6).fill(<div>no key</div>)}</Parent>, | 1400 | 1414 | ); | 1401 | 1415 | ReactNoopFlightClient.read(transport); | | 1416 | + await act(async () => { | | 1417 | + ReactNoop.render(await ReactNoopFlightClient.read(transport)); | | 1418 | + }); | 1402 | 1419 | }).toErrorDev( | 1403 | | - 'Each child in a list should have a unique "key" prop. ' + | 1404 | | - 'See https://react.dev/link/warning-keys for more information.', | | 1420 | + gate(flags => flags.enableOwnerStacks) | | 1421 | + ? 'Each child in a list should have a unique "key" prop.' + | | 1422 | + '\n\nCheck the top-level render call using <ParentClient>. ' + | | 1423 | + 'See https://react.dev/link/warning-keys for more information.' | | 1424 | + : 'Each child in a list should have a unique "key" prop. ' + | | 1425 | + 'See https://react.dev/link/warning-keys for more information.', | 1405 | 1426 | ); | 1406 | 1427 | }); | 1407 | 1428 |
| @@ -2309,7 +2330,7 @@ describe('ReactFlight', () => { | 2309 | 2330 | } | 2310 | 2331 |
| 2311 | 2332 | function ThirdPartyFragmentComponent() { | 2312 | | - return [<span>Who</span>, ' ', <span>dis?</span>]; | | 2333 | + return [<span key="1">Who</span>, ' ', <span key="2">dis?</span>]; | 2313 | 2334 | } | 2314 | 2335 |
| 2315 | 2336 | function ServerComponent({transport}) { | @@ -2321,7 +2342,7 @@ describe('ReactFlight', () => { | 2321 | 2342 | const promiseComponent = Promise.resolve(<ThirdPartyComponent />); | 2322 | 2343 |
| 2323 | 2344 | const thirdPartyTransport = ReactNoopFlightServer.render( | 2324 | | - [promiseComponent, lazy, <ThirdPartyFragmentComponent />], | | 2345 | + [promiseComponent, lazy, <ThirdPartyFragmentComponent key="3" />], | 2325 | 2346 | { | 2326 | 2347 | environmentName: 'third-party', | 2327 | 2348 | }, | @@ -2413,8 +2434,8 @@ describe('ReactFlight', () => { | 2413 | 2434 | const iteratorPromise = new Promise(r => (resolve = r)); | 2414 | 2435 |
| 2415 | 2436 | async function* ThirdPartyAsyncIterableComponent({item, initial}) { | 2416 | | - yield <span>Who</span>; | 2417 | | - yield <span>dis?</span>; | | 2437 | + yield <span key="1">Who</span>; | | 2438 | + yield <span key="2">dis?</span>; | 2418 | 2439 | resolve(); | 2419 | 2440 | } | 2420 | 2441 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMBrowser-test.js +10-5Lines changed: 10 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -645,6 +645,10 @@ describe('ReactFlightDOMBrowser', () => { | 645 | 645 | } | 646 | 646 | const Parent = clientExports(ParentClient); | 647 | 647 | const ParentModule = clientExports({Parent: ParentClient}); | | 648 | + | | 649 | + const container = document.createElement('div'); | | 650 | + const root = ReactDOMClient.createRoot(container); | | 651 | + | 648 | 652 | await expect(async () => { | 649 | 653 | const stream = ReactServerDOMServer.renderToReadableStream( | 650 | 654 | <> | @@ -655,11 +659,12 @@ describe('ReactFlightDOMBrowser', () => { | 655 | 659 | </>, | 656 | 660 | webpackMap, | 657 | 661 | ); | 658 | | - await ReactServerDOMClient.createFromReadableStream(stream); | 659 | | - }).toErrorDev( | 660 | | - 'Each child in a list should have a unique "key" prop. ' + | 661 | | - 'See https://react.dev/link/warning-keys for more information.', | 662 | | - ); | | 662 | + const result = | | 663 | + await ReactServerDOMClient.createFromReadableStream(stream); | | 664 | + await act(() => { | | 665 | + root.render(result); | | 666 | + }); | | 667 | + }).toErrorDev('Each child in a list should have a unique "key" prop.'); | 663 | 668 | }); | 664 | 669 |
| 665 | 670 | it('basic use(promise)', async () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMEdge-test.js +70-2Lines changed: 70 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -256,7 +256,41 @@ describe('ReactFlightDOMEdge', () => { | 256 | 256 | return str; | 257 | 257 | } | 258 | 258 | const element = <ServerComponent />; | 259 | | - const children = new Array(30).fill(element); | | 259 | + // Hardcoded list to avoid the key warning | | 260 | + const children = ( | | 261 | + <> | | 262 | + {element} | | 263 | + {element} | | 264 | + {element} | | 265 | + {element} | | 266 | + {element} | | 267 | + {element} | | 268 | + {element} | | 269 | + {element} | | 270 | + {element} | | 271 | + {element} | | 272 | + {element} | | 273 | + {element} | | 274 | + {element} | | 275 | + {element} | | 276 | + {element} | | 277 | + {element} | | 278 | + {element} | | 279 | + {element} | | 280 | + {element} | | 281 | + {element} | | 282 | + {element} | | 283 | + {element} | | 284 | + {element} | | 285 | + {element} | | 286 | + {element} | | 287 | + {element} | | 288 | + {element} | | 289 | + {element} | | 290 | + {element} | | 291 | + {element} | | 292 | + </> | | 293 | + ); | 260 | 294 | const resolvedChildren = new Array(30).fill(str); | 261 | 295 | const stream = ReactServerDOMServer.renderToReadableStream(children); | 262 | 296 | const [stream1, stream2] = passThrough(stream).tee(); | @@ -288,7 +322,41 @@ describe('ReactFlightDOMEdge', () => { | 288 | 322 | return div; | 289 | 323 | } | 290 | 324 | const element = <ServerComponent />; | 291 | | - const children = new Array(30).fill(element); | | 325 | + // Hardcoded list to avoid the key warning | | 326 | + const children = ( | | 327 | + <> | | 328 | + {element} | | 329 | + {element} | | 330 | + {element} | | 331 | + {element} | | 332 | + {element} | | 333 | + {element} | | 334 | + {element} | | 335 | + {element} | | 336 | + {element} | | 337 | + {element} | | 338 | + {element} | | 339 | + {element} | | 340 | + {element} | | 341 | + {element} | | 342 | + {element} | | 343 | + {element} | | 344 | + {element} | | 345 | + {element} | | 346 | + {element} | | 347 | + {element} | | 348 | + {element} | | 349 | + {element} | | 350 | + {element} | | 351 | + {element} | | 352 | + {element} | | 353 | + {element} | | 354 | + {element} | | 355 | + {element} | | 356 | + {element} | | 357 | + {element} | | 358 | + </> | | 359 | + ); | 292 | 360 | const resolvedChildren = new Array(30).fill( | 293 | 361 | '<div>this is a long return value</div>', | 294 | 362 | ); | |
|