Skip to content

Commit 3682021

Browse files
authoredApr 21, 2024··
Warn for Child Iterator of all types but allow Generator Components (#28853)
This doesn't change production behavior. We always render Iterables to our best effort in prod even if they're Iterators. But this does change the DEV warnings which indicates which are valid patterns to use. It's a footgun to use an Iterator as a prop when you pass between components because if an intermediate component rerenders without its parent, React won't be able to iterate it again to reconcile and any mappers won't be able to re-apply. This is actually typically not a problem when passed only to React host components but as a pattern it's a problem for composability. We used to warn only for Generators - i.e. Iterators returned from Generator functions. This adds a warning for Iterators created by other means too (e.g. Flight or the native Iterator utils). The heuristic is to check whether the Iterator is the same as the Iterable because that means it's not possible to get new iterators out of it. This case used to just yield non-sense like empty sets in DEV but not in prod. However, a new realization is that when the Component itself is a Generator Function, it's not actually a problem. That's because the React Element itself works as an Iterable since we can ask for new generators by calling the function again. So this adds a special case to allow the Generator returned from a Generator Function's direct child. The principle is “don’t pass iterators around” but in this case there is no iterator floating around because it’s between React and the JS VM. Also see #28849 for context on AsyncIterables. Related to this, but Hooks should ideally be banned in these for the same reason they're banned in Async Functions.
1 parent 857ee8c commit 3682021

File tree

5 files changed

+262
-73
lines changed

5 files changed

+262
-73
lines changed
 

‎packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

+81
Original file line numberDiff line numberDiff line change
@@ -7984,4 +7984,85 @@ describe('ReactDOMFizzServer', () => {
79847984
]);
79857985
expect(postpones).toEqual([]);
79867986
});
7987+
7988+
it('should NOT warn for using generator functions as components', async () => {
7989+
function* Foo() {
7990+
yield <h1 key="1">Hello</h1>;
7991+
yield <h1 key="2">World</h1>;
7992+
}
7993+
7994+
await act(() => {
7995+
const {pipe} = renderToPipeableStream(<Foo />);
7996+
pipe(writable);
7997+
});
7998+
7999+
expect(document.body.textContent).toBe('HelloWorld');
8000+
});
8001+
8002+
it('should warn for using generators as children props', async () => {
8003+
function* getChildren() {
8004+
yield <h1 key="1">Hello</h1>;
8005+
yield <h1 key="2">World</h1>;
8006+
}
8007+
8008+
function Foo() {
8009+
const children = getChildren();
8010+
return <div>{children}</div>;
8011+
}
8012+
8013+
await expect(async () => {
8014+
await act(() => {
8015+
const {pipe} = renderToPipeableStream(<Foo />);
8016+
pipe(writable);
8017+
});
8018+
}).toErrorDev(
8019+
'Using Iterators as children is unsupported and will likely yield ' +
8020+
'unexpected results because enumerating a generator mutates it. ' +
8021+
'You may convert it to an array with `Array.from()` or the ' +
8022+
'`[...spread]` operator before rendering. You can also use an ' +
8023+
'Iterable that can iterate multiple times over the same items.\n' +
8024+
' in div (at **)\n' +
8025+
' in Foo (at **)',
8026+
);
8027+
8028+
expect(document.body.textContent).toBe('HelloWorld');
8029+
});
8030+
8031+
it('should warn for using other types of iterators as children', async () => {
8032+
function Foo() {
8033+
let i = 0;
8034+
const iterator = {
8035+
[Symbol.iterator]() {
8036+
return iterator;
8037+
},
8038+
next() {
8039+
switch (i++) {
8040+
case 0:
8041+
return {done: false, value: <h1 key="1">Hello</h1>};
8042+
case 1:
8043+
return {done: false, value: <h1 key="2">World</h1>};
8044+
default:
8045+
return {done: true, value: undefined};
8046+
}
8047+
},
8048+
};
8049+
return iterator;
8050+
}
8051+
8052+
await expect(async () => {
8053+
await act(() => {
8054+
const {pipe} = renderToPipeableStream(<Foo />);
8055+
pipe(writable);
8056+
});
8057+
}).toErrorDev(
8058+
'Using Iterators as children is unsupported and will likely yield ' +
8059+
'unexpected results because enumerating a generator mutates it. ' +
8060+
'You may convert it to an array with `Array.from()` or the ' +
8061+
'`[...spread]` operator before rendering. You can also use an ' +
8062+
'Iterable that can iterate multiple times over the same items.\n' +
8063+
' in Foo (at **)',
8064+
);
8065+
8066+
expect(document.body.textContent).toBe('HelloWorld');
8067+
});
79878068
});

‎packages/react-dom/src/__tests__/ReactMultiChild-test.js

+73-5
Original file line numberDiff line numberDiff line change
@@ -328,26 +328,94 @@ describe('ReactMultiChild', () => {
328328
);
329329
});
330330

331-
it('should warn for using generators as children', async () => {
331+
it('should NOT warn for using generator functions as components', async () => {
332332
function* Foo() {
333333
yield <h1 key="1">Hello</h1>;
334334
yield <h1 key="2">World</h1>;
335335
}
336336

337+
const container = document.createElement('div');
338+
const root = ReactDOMClient.createRoot(container);
339+
await act(async () => {
340+
root.render(<Foo />);
341+
});
342+
343+
expect(container.textContent).toBe('HelloWorld');
344+
});
345+
346+
it('should warn for using generators as children props', async () => {
347+
function* getChildren() {
348+
yield <h1 key="1">Hello</h1>;
349+
yield <h1 key="2">World</h1>;
350+
}
351+
352+
function Foo() {
353+
const children = getChildren();
354+
return <div>{children}</div>;
355+
}
356+
357+
const container = document.createElement('div');
358+
const root = ReactDOMClient.createRoot(container);
359+
await expect(async () => {
360+
await act(async () => {
361+
root.render(<Foo />);
362+
});
363+
}).toErrorDev(
364+
'Using Iterators as children is unsupported and will likely yield ' +
365+
'unexpected results because enumerating a generator mutates it. ' +
366+
'You may convert it to an array with `Array.from()` or the ' +
367+
'`[...spread]` operator before rendering. You can also use an ' +
368+
'Iterable that can iterate multiple times over the same items.\n' +
369+
' in div (at **)\n' +
370+
' in Foo (at **)',
371+
);
372+
373+
expect(container.textContent).toBe('HelloWorld');
374+
375+
// Test de-duplication
376+
await act(async () => {
377+
root.render(<Foo />);
378+
});
379+
});
380+
381+
it('should warn for using other types of iterators as children', async () => {
382+
function Foo() {
383+
let i = 0;
384+
const iterator = {
385+
[Symbol.iterator]() {
386+
return iterator;
387+
},
388+
next() {
389+
switch (i++) {
390+
case 0:
391+
return {done: false, value: <h1 key="1">Hello</h1>};
392+
case 1:
393+
return {done: false, value: <h1 key="2">World</h1>};
394+
default:
395+
return {done: true, value: undefined};
396+
}
397+
},
398+
};
399+
return iterator;
400+
}
401+
337402
const container = document.createElement('div');
338403
const root = ReactDOMClient.createRoot(container);
339404
await expect(async () => {
340405
await act(async () => {
341406
root.render(<Foo />);
342407
});
343408
}).toErrorDev(
344-
'Using Generators as children is unsupported and will likely yield ' +
345-
'unexpected results because enumerating a generator mutates it. You may ' +
346-
'convert it to an array with `Array.from()` or the `[...spread]` operator ' +
347-
'before rendering. Keep in mind you might need to polyfill these features for older browsers.\n' +
409+
'Using Iterators as children is unsupported and will likely yield ' +
410+
'unexpected results because enumerating a generator mutates it. ' +
411+
'You may convert it to an array with `Array.from()` or the ' +
412+
'`[...spread]` operator before rendering. You can also use an ' +
413+
'Iterable that can iterate multiple times over the same items.\n' +
348414
' in Foo (at **)',
349415
);
350416

417+
expect(container.textContent).toBe('HelloWorld');
418+
351419
// Test de-duplication
352420
await act(async () => {
353421
root.render(<Foo />);

‎packages/react-reconciler/src/ReactChildFiber.js

+64-39
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ import {
3333
REACT_LAZY_TYPE,
3434
REACT_CONTEXT_TYPE,
3535
} from 'shared/ReactSymbols';
36-
import {HostRoot, HostText, HostPortal, Fragment} from './ReactWorkTags';
36+
import {
37+
HostRoot,
38+
HostText,
39+
HostPortal,
40+
Fragment,
41+
FunctionComponent,
42+
} from './ReactWorkTags';
3743
import isArray from 'shared/isArray';
3844
import {enableRefAsProp} from 'shared/ReactFeatureFlags';
3945

@@ -1114,52 +1120,46 @@ function createChildReconciler(
11141120
);
11151121
}
11161122

1123+
const newChildren = iteratorFn.call(newChildrenIterable);
1124+
11171125
if (__DEV__) {
1118-
// We don't support rendering Generators because it's a mutation.
1119-
// See https://github.com/facebook/react/issues/12995
1120-
if (
1121-
typeof Symbol === 'function' &&
1122-
// $FlowFixMe[prop-missing] Flow doesn't know about toStringTag
1123-
newChildrenIterable[Symbol.toStringTag] === 'Generator'
1124-
) {
1125-
if (!didWarnAboutGenerators) {
1126-
console.error(
1127-
'Using Generators as children is unsupported and will likely yield ' +
1128-
'unexpected results because enumerating a generator mutates it. ' +
1129-
'You may convert it to an array with `Array.from()` or the ' +
1130-
'`[...spread]` operator before rendering. Keep in mind ' +
1131-
'you might need to polyfill these features for older browsers.',
1132-
);
1126+
if (newChildren === newChildrenIterable) {
1127+
// We don't support rendering Generators as props because it's a mutation.
1128+
// See https://github.com/facebook/react/issues/12995
1129+
// We do support generators if they were created by a GeneratorFunction component
1130+
// as its direct child since we can recreate those by rerendering the component
1131+
// as needed.
1132+
const isGeneratorComponent =
1133+
returnFiber.tag === FunctionComponent &&
1134+
// $FlowFixMe[method-unbinding]
1135+
Object.prototype.toString.call(returnFiber.type) ===
1136+
'[object GeneratorFunction]' &&
1137+
// $FlowFixMe[method-unbinding]
1138+
Object.prototype.toString.call(newChildren) === '[object Generator]';
1139+
if (!isGeneratorComponent) {
1140+
if (!didWarnAboutGenerators) {
1141+
console.error(
1142+
'Using Iterators as children is unsupported and will likely yield ' +
1143+
'unexpected results because enumerating a generator mutates it. ' +
1144+
'You may convert it to an array with `Array.from()` or the ' +
1145+
'`[...spread]` operator before rendering. You can also use an ' +
1146+
'Iterable that can iterate multiple times over the same items.',
1147+
);
1148+
}
1149+
didWarnAboutGenerators = true;
11331150
}
1134-
didWarnAboutGenerators = true;
1135-
}
1136-
1137-
// Warn about using Maps as children
1138-
if ((newChildrenIterable: any).entries === iteratorFn) {
1151+
} else if ((newChildrenIterable: any).entries === iteratorFn) {
1152+
// Warn about using Maps as children
11391153
if (!didWarnAboutMaps) {
11401154
console.error(
11411155
'Using Maps as children is not supported. ' +
11421156
'Use an array of keyed ReactElements instead.',
11431157
);
1144-
}
1145-
didWarnAboutMaps = true;
1146-
}
1147-
1148-
// First, validate keys.
1149-
// We'll get a different iterator later for the main pass.
1150-
const newChildren = iteratorFn.call(newChildrenIterable);
1151-
if (newChildren) {
1152-
let knownKeys: Set<string> | null = null;
1153-
let step = newChildren.next();
1154-
for (; !step.done; step = newChildren.next()) {
1155-
const child = step.value;
1156-
knownKeys = warnOnInvalidKey(child, knownKeys, returnFiber);
1158+
didWarnAboutMaps = true;
11571159
}
11581160
}
11591161
}
11601162

1161-
const newChildren = iteratorFn.call(newChildrenIterable);
1162-
11631163
if (newChildren == null) {
11641164
throw new Error('An iterable object provided no iterator.');
11651165
}
@@ -1172,11 +1172,20 @@ function createChildReconciler(
11721172
let newIdx = 0;
11731173
let nextOldFiber = null;
11741174

1175+
let knownKeys: Set<string> | null = null;
1176+
11751177
let step = newChildren.next();
1178+
if (__DEV__) {
1179+
knownKeys = warnOnInvalidKey(step.value, knownKeys, returnFiber);
1180+
}
11761181
for (
11771182
;
11781183
oldFiber !== null && !step.done;
1179-
newIdx++, step = newChildren.next()
1184+
newIdx++,
1185+
step = newChildren.next(),
1186+
knownKeys = __DEV__
1187+
? warnOnInvalidKey(step.value, knownKeys, returnFiber)
1188+
: null
11801189
) {
11811190
if (oldFiber.index > newIdx) {
11821191
nextOldFiber = oldFiber;
@@ -1236,7 +1245,15 @@ function createChildReconciler(
12361245
if (oldFiber === null) {
12371246
// If we don't have any more existing children we can choose a fast path
12381247
// since the rest will all be insertions.
1239-
for (; !step.done; newIdx++, step = newChildren.next()) {
1248+
for (
1249+
;
1250+
!step.done;
1251+
newIdx++,
1252+
step = newChildren.next(),
1253+
knownKeys = __DEV__
1254+
? warnOnInvalidKey(step.value, knownKeys, returnFiber)
1255+
: null
1256+
) {
12401257
const newFiber = createChild(returnFiber, step.value, lanes, debugInfo);
12411258
if (newFiber === null) {
12421259
continue;
@@ -1261,7 +1278,15 @@ function createChildReconciler(
12611278
const existingChildren = mapRemainingChildren(oldFiber);
12621279

12631280
// Keep scanning and use the map to restore deleted items as moves.
1264-
for (; !step.done; newIdx++, step = newChildren.next()) {
1281+
for (
1282+
;
1283+
!step.done;
1284+
newIdx++,
1285+
step = newChildren.next(),
1286+
knownKeys = __DEV__
1287+
? warnOnInvalidKey(step.value, knownKeys, returnFiber)
1288+
: null
1289+
) {
12651290
const newFiber = updateFromMap(
12661291
existingChildren,
12671292
returnFiber,

0 commit comments

Comments
 (0)
Please sign in to comment.