GeezerVison is showing github.com — Validate key in Flight · react/react@5a71659
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, | @@ -1034,6 +1035,7 @@ function parseModelTuple( | 1034 | 1035 | tuple[3], | 1035 | 1036 | __DEV__ ? (tuple: any)[4] : null, | 1036 | 1037 | __DEV__ && enableOwnerStacks ? (tuple: any)[5] : null, | | 1038 | + __DEV__ && enableOwnerStacks ? (tuple: any)[6] : 0, | 1037 | 1039 | ); | 1038 | 1040 | } | 1039 | 1041 | 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 |
|---|
@@ -596,6 +596,10 @@ describe('ReactFlightDOMBrowser', () => { | 596 | 596 | } | 597 | 597 | const Parent = clientExports(ParentClient); | 598 | 598 | const ParentModule = clientExports({Parent: ParentClient}); | | 599 | + | | 600 | + const container = document.createElement('div'); | | 601 | + const root = ReactDOMClient.createRoot(container); | | 602 | + | 599 | 603 | await expect(async () => { | 600 | 604 | const stream = ReactServerDOMServer.renderToReadableStream( | 601 | 605 | <> | @@ -606,11 +610,12 @@ describe('ReactFlightDOMBrowser', () => { | 606 | 610 | </>, | 607 | 611 | webpackMap, | 608 | 612 | ); | 609 | | - await ReactServerDOMClient.createFromReadableStream(stream); | 610 | | - }).toErrorDev( | 611 | | - 'Each child in a list should have a unique "key" prop. ' + | 612 | | - 'See https://react.dev/link/warning-keys for more information.', | 613 | | - ); | | 613 | + const result = | | 614 | + await ReactServerDOMClient.createFromReadableStream(stream); | | 615 | + await act(() => { | | 616 | + root.render(result); | | 617 | + }); | | 618 | + }).toErrorDev('Each child in a list should have a unique "key" prop.'); | 614 | 619 | }); | 615 | 620 |
| 616 | 621 | it('basic use(promise)', async () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/ReactFlightServer.js +97-6Lines changed: 97 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -399,6 +399,7 @@ export type Request = { | 399 | 399 | onPostpone: (reason: string) => void, | 400 | 400 | // DEV-only | 401 | 401 | environmentName: string, | | 402 | + didWarnForKey: null | WeakSet<ReactComponentInfo>, | 402 | 403 | }; | 403 | 404 |
| 404 | 405 | const { | @@ -500,6 +501,7 @@ export function createRequest( | 500 | 501 | if (__DEV__) { | 501 | 502 | request.environmentName = | 502 | 503 | environmentName === undefined ? 'Server' : environmentName; | | 504 | + request.didWarnForKey = null; | 503 | 505 | } | 504 | 506 | const rootTask = createTask(request, model, null, false, abortSet); | 505 | 507 | pingedTasks.push(rootTask); | @@ -965,6 +967,7 @@ function renderFunctionComponent<Props>( | 965 | 967 | props: Props, | 966 | 968 | owner: null | ReactComponentInfo, // DEV-only | 967 | 969 | stack: null | string, // DEV-only | | 970 | + validated: number, // DEV-only | 968 | 971 | ): ReactJSONValue { | 969 | 972 | // Reset the task's thenable state before continuing, so that if a later | 970 | 973 | // component suspends we can reuse the same task object. If the same | @@ -1005,6 +1008,10 @@ function renderFunctionComponent<Props>( | 1005 | 1008 | // being no references to this as an owner. | 1006 | 1009 | outlineModel(request, componentDebugInfo); | 1007 | 1010 | emitDebugChunk(request, componentDebugID, componentDebugInfo); | | 1011 | + | | 1012 | + if (enableOwnerStacks) { | | 1013 | + warnForMissingKey(request, key, validated, componentDebugInfo); | | 1014 | + } | 1008 | 1015 | } | 1009 | 1016 | prepareToUseHooksForComponent(prevThenableState, componentDebugInfo); | 1010 | 1017 | result = callComponentInDEV(Component, props, componentDebugInfo); | @@ -1102,6 +1109,8 @@ function renderFunctionComponent<Props>( | 1102 | 1109 | if (__DEV__) { | 1103 | 1110 | (result: any)._debugInfo = iterableChild._debugInfo; | 1104 | 1111 | } | | 1112 | + } else if (__DEV__ && (result: any).$$typeof === REACT_ELEMENT_TYPE) { | | 1113 | + (result: any)._store.validated = 1; | 1105 | 1114 | } | 1106 | 1115 | } | 1107 | 1116 | // Track this element's key on the Server Component on the keyPath context.. | @@ -1124,11 +1133,68 @@ function renderFunctionComponent<Props>( | 1124 | 1133 | return json; | 1125 | 1134 | } | 1126 | 1135 |
| | 1136 | +function warnForMissingKey( | | 1137 | + request: Request, | | 1138 | + key: null | string, | | 1139 | + validated: number, | | 1140 | + componentDebugInfo: ReactComponentInfo, | | 1141 | +): void { | | 1142 | + if (__DEV__) { | | 1143 | + if (validated !== 2) { | | 1144 | + return; | | 1145 | + } | | 1146 | + | | 1147 | + let didWarnForKey = request.didWarnForKey; | | 1148 | + if (didWarnForKey == null) { | | 1149 | + didWarnForKey = request.didWarnForKey = new WeakSet(); | | 1150 | + } | | 1151 | + const parentOwner = componentDebugInfo.owner; | | 1152 | + if (parentOwner != null) { | | 1153 | + if (didWarnForKey.has(parentOwner)) { | | 1154 | + // We already warned for other children in this parent. | | 1155 | + return; | | 1156 | + } | | 1157 | + didWarnForKey.add(parentOwner); | | 1158 | + } | | 1159 | + | | 1160 | + // Call with the server component as the currently rendering component | | 1161 | + // for context. | | 1162 | + callComponentInDEV( | | 1163 | + () => { | | 1164 | + console.error( | | 1165 | + 'Each child in a list should have a unique "key" prop.' + | | 1166 | + '%s%s See https://react.dev/link/warning-keys for more information.', | | 1167 | + '', | | 1168 | + '', | | 1169 | + ); | | 1170 | + }, | | 1171 | + null, | | 1172 | + componentDebugInfo, | | 1173 | + ); | | 1174 | + } | | 1175 | +} | | 1176 | + | 1127 | 1177 | function renderFragment( | 1128 | 1178 | request: Request, | 1129 | 1179 | task: Task, | 1130 | 1180 | children: $ReadOnlyArray<ReactClientValue>, | 1131 | 1181 | ): ReactJSONValue { | | 1182 | + if (__DEV__) { | | 1183 | + for (let i = 0; i < children.length; i++) { | | 1184 | + const child = children[i]; | | 1185 | + if ( | | 1186 | + child !== null && | | 1187 | + typeof child === 'object' && | | 1188 | + child.$$typeof === REACT_ELEMENT_TYPE | | 1189 | + ) { | | 1190 | + const element: ReactElement = (child: any); | | 1191 | + if (element.key === null && !element._store.validated) { | | 1192 | + element._store.validated = 2; | | 1193 | + } | | 1194 | + } | | 1195 | + } | | 1196 | + } | | 1197 | + | 1132 | 1198 | if (task.keyPath !== null) { | 1133 | 1199 | // We have a Server Component that specifies a key but we're now splitting | 1134 | 1200 | // the tree using a fragment. | @@ -1231,6 +1297,7 @@ function renderClientElement( | 1231 | 1297 | props: any, | 1232 | 1298 | owner: null | ReactComponentInfo, // DEV-only | 1233 | 1299 | stack: null | string, // DEV-only | | 1300 | + validated: number, // DEV-only | 1234 | 1301 | ): ReactJSONValue { | 1235 | 1302 | // We prepend the terminal client element that actually gets serialized with | 1236 | 1303 | // the keys of any Server Components which are not serialized. | @@ -1242,7 +1309,7 @@ function renderClientElement( | 1242 | 1309 | } | 1243 | 1310 | const element = __DEV__ | 1244 | 1311 | ? enableOwnerStacks | 1245 | | - ? [REACT_ELEMENT_TYPE, type, key, props, owner, stack] | | 1312 | + ? [REACT_ELEMENT_TYPE, type, key, props, owner, stack, validated] | 1246 | 1313 | : [REACT_ELEMENT_TYPE, type, key, props, owner] | 1247 | 1314 | : [REACT_ELEMENT_TYPE, type, key, props]; | 1248 | 1315 | if (task.implicitSlot && key !== null) { | @@ -1292,6 +1359,7 @@ function renderElement( | 1292 | 1359 | props: any, | 1293 | 1360 | owner: null | ReactComponentInfo, // DEV only | 1294 | 1361 | stack: null | string, // DEV only | | 1362 | + validated: number, // DEV only | 1295 | 1363 | ): ReactJSONValue { | 1296 | 1364 | if (ref !== null && ref !== undefined) { | 1297 | 1365 | // When the ref moves to the regular props object this will implicitly | @@ -1312,7 +1380,15 @@ function renderElement( | 1312 | 1380 | if (typeof type === 'function') { | 1313 | 1381 | if (isClientReference(type) || isOpaqueTemporaryReference(type)) { | 1314 | 1382 | // This is a reference to a Client Component. | 1315 | | - return renderClientElement(task, type, key, props, owner, stack); | | 1383 | + return renderClientElement( | | 1384 | + task, | | 1385 | + type, | | 1386 | + key, | | 1387 | + props, | | 1388 | + owner, | | 1389 | + stack, | | 1390 | + validated, | | 1391 | + ); | 1316 | 1392 | } | 1317 | 1393 | // This is a Server Component. | 1318 | 1394 | return renderFunctionComponent( | @@ -1323,10 +1399,11 @@ function renderElement( | 1323 | 1399 | props, | 1324 | 1400 | owner, | 1325 | 1401 | stack, | | 1402 | + validated, | 1326 | 1403 | ); | 1327 | 1404 | } else if (typeof type === 'string') { | 1328 | 1405 | // This is a host element. E.g. HTML. | 1329 | | - return renderClientElement(task, type, key, props, owner, stack); | | 1406 | + return renderClientElement(task, type, key, props, owner, stack, validated); | 1330 | 1407 | } else if (typeof type === 'symbol') { | 1331 | 1408 | if (type === REACT_FRAGMENT_TYPE && key === null) { | 1332 | 1409 | // For key-less fragments, we add a small optimization to avoid serializing | @@ -1347,11 +1424,19 @@ function renderElement( | 1347 | 1424 | } | 1348 | 1425 | // This might be a built-in React component. We'll let the client decide. | 1349 | 1426 | // Any built-in works as long as its props are serializable. | 1350 | | - return renderClientElement(task, type, key, props, owner, stack); | | 1427 | + return renderClientElement(task, type, key, props, owner, stack, validated); | 1351 | 1428 | } else if (type != null && typeof type === 'object') { | 1352 | 1429 | if (isClientReference(type)) { | 1353 | 1430 | // This is a reference to a Client Component. | 1354 | | - return renderClientElement(task, type, key, props, owner, stack); | | 1431 | + return renderClientElement( | | 1432 | + task, | | 1433 | + type, | | 1434 | + key, | | 1435 | + props, | | 1436 | + owner, | | 1437 | + stack, | | 1438 | + validated, | | 1439 | + ); | 1355 | 1440 | } | 1356 | 1441 | switch (type.$$typeof) { | 1357 | 1442 | case REACT_LAZY_TYPE: { | @@ -1372,6 +1457,7 @@ function renderElement( | 1372 | 1457 | props, | 1373 | 1458 | owner, | 1374 | 1459 | stack, | | 1460 | + validated, | 1375 | 1461 | ); | 1376 | 1462 | } | 1377 | 1463 | case REACT_FORWARD_REF_TYPE: { | @@ -1383,6 +1469,7 @@ function renderElement( | 1383 | 1469 | props, | 1384 | 1470 | owner, | 1385 | 1471 | stack, | | 1472 | + validated, | 1386 | 1473 | ); | 1387 | 1474 | } | 1388 | 1475 | case REACT_MEMO_TYPE: { | @@ -1395,6 +1482,7 @@ function renderElement( | 1395 | 1482 | props, | 1396 | 1483 | owner, | 1397 | 1484 | stack, | | 1485 | + validated, | 1398 | 1486 | ); | 1399 | 1487 | } | 1400 | 1488 | } | @@ -1963,8 +2051,11 @@ function renderModelDestructive( | 1963 | 2051 | props, | 1964 | 2052 | __DEV__ ? element._owner : null, | 1965 | 2053 | __DEV__ && enableOwnerStacks | 1966 | | - ? filterDebugStack(element._debugStack) | | 2054 | + ? !element._debugStack || typeof element._debugStack === 'string' | | 2055 | + ? element._debugStack | | 2056 | + : filterDebugStack(element._debugStack) | 1967 | 2057 | : null, | | 2058 | + __DEV__ && enableOwnerStacks ? element._store.validated : 0, | 1968 | 2059 | ); | 1969 | 2060 | } | 1970 | 2061 | case REACT_LAZY_TYPE: { | |
|