Skip to content

Commit

Permalink
Warn for Child Iterator of all types but allow Generator Components (#…
Browse files Browse the repository at this point in the history
…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.

DiffTrain build for [3682021](3682021)
  • Loading branch information
sebmarkbage committed Apr 21, 2024
1 parent 1c83803 commit 91c2cfe
Show file tree
Hide file tree
Showing 29 changed files with 467 additions and 474 deletions.
11 changes: 7 additions & 4 deletions compiled/facebook-www/JSXDEVRuntime-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1388,11 +1388,14 @@ function validateChildKeys(node, parentType) {
// but now we print a separate warning for them later.
if (iteratorFn !== node.entries) {
var iterator = iteratorFn.call(node);
var step;

while (!(step = iterator.next()).done) {
if (isValidElement(step.value)) {
validateExplicitKey(step.value, parentType);
if (iterator !== node) {
var step;

while (!(step = iterator.next()).done) {
if (isValidElement(step.value)) {
validateExplicitKey(step.value, parentType);
}
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions compiled/facebook-www/JSXDEVRuntime-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -1391,11 +1391,14 @@ function validateChildKeys(node, parentType) {
// but now we print a separate warning for them later.
if (iteratorFn !== node.entries) {
var iterator = iteratorFn.call(node);
var step;

while (!(step = iterator.next()).done) {
if (isValidElement(step.value)) {
validateExplicitKey(step.value, parentType);
if (iterator !== node) {
var step;

while (!(step = iterator.next()).done) {
if (isValidElement(step.value)) {
validateExplicitKey(step.value, parentType);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
857ee8cdf9af81bc94a7f04528fbda7fb2510eb4
368202181e772d411b2445930aea1edd9428b09b
13 changes: 8 additions & 5 deletions compiled/facebook-www/React-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (
) {
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
}
var ReactVersion = '19.0.0-www-classic-8ac39151';
var ReactVersion = '19.0.0-www-classic-29916c01';

// ATTENTION
// When adding new symbols to this file,
Expand Down Expand Up @@ -1963,11 +1963,14 @@ function validateChildKeys(node, parentType) {
// but now we print a separate warning for them later.
if (iteratorFn !== node.entries) {
var iterator = iteratorFn.call(node);
var step;

while (!(step = iterator.next()).done) {
if (isValidElement(step.value)) {
validateExplicitKey(step.value, parentType);
if (iterator !== node) {
var step;

while (!(step = iterator.next()).done) {
if (isValidElement(step.value)) {
validateExplicitKey(step.value, parentType);
}
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions compiled/facebook-www/React-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (
) {
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
}
var ReactVersion = '19.0.0-www-modern-f6026193';
var ReactVersion = '19.0.0-www-modern-16b8232b';

// ATTENTION
// When adding new symbols to this file,
Expand Down Expand Up @@ -1966,11 +1966,14 @@ function validateChildKeys(node, parentType) {
// but now we print a separate warning for them later.
if (iteratorFn !== node.entries) {
var iterator = iteratorFn.call(node);
var step;

while (!(step = iterator.next()).done) {
if (isValidElement(step.value)) {
validateExplicitKey(step.value, parentType);
if (iterator !== node) {
var step;

while (!(step = iterator.next()).done) {
if (isValidElement(step.value)) {
validateExplicitKey(step.value, parentType);
}
}
}
}
Expand Down
64 changes: 30 additions & 34 deletions compiled/facebook-www/ReactART-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = '19.0.0-www-classic-d14dfff2';
var ReactVersion = '19.0.0-www-classic-ff6202ef';

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -6987,45 +6987,36 @@ function createChildReconciler(shouldTrackSideEffects) {
throw new Error('An object is not an iterable. This error is likely caused by a bug in ' + 'React. Please file an issue.');
}

{
// We don't support rendering Generators because it's a mutation.
// See https://github.com/facebook/react/issues/12995
if (typeof Symbol === 'function' && // $FlowFixMe[prop-missing] Flow doesn't know about toStringTag
newChildrenIterable[Symbol.toStringTag] === 'Generator') {
if (!didWarnAboutGenerators) {
error('Using Generators as children is unsupported and will likely yield ' + 'unexpected results because enumerating a generator mutates it. ' + 'You may convert it to an array with `Array.from()` or the ' + '`[...spread]` operator before rendering. Keep in mind ' + 'you might need to polyfill these features for older browsers.');
}

didWarnAboutGenerators = true;
} // Warn about using Maps as children
var newChildren = iteratorFn.call(newChildrenIterable);

{
if (newChildren === newChildrenIterable) {
// We don't support rendering Generators as props because it's a mutation.
// See https://github.com/facebook/react/issues/12995
// We do support generators if they were created by a GeneratorFunction component
// as its direct child since we can recreate those by rerendering the component
// as needed.
var isGeneratorComponent = returnFiber.tag === FunctionComponent && // $FlowFixMe[method-unbinding]
Object.prototype.toString.call(returnFiber.type) === '[object GeneratorFunction]' && // $FlowFixMe[method-unbinding]
Object.prototype.toString.call(newChildren) === '[object Generator]';

if (!isGeneratorComponent) {
if (!didWarnAboutGenerators) {
error('Using Iterators as children is unsupported and will likely yield ' + 'unexpected results because enumerating a generator mutates it. ' + 'You may convert it to an array with `Array.from()` or the ' + '`[...spread]` operator before rendering. You can also use an ' + 'Iterable that can iterate multiple times over the same items.');
}

if (newChildrenIterable.entries === iteratorFn) {
didWarnAboutGenerators = true;
}
} else if (newChildrenIterable.entries === iteratorFn) {
// Warn about using Maps as children
if (!didWarnAboutMaps) {
error('Using Maps as children is not supported. ' + 'Use an array of keyed ReactElements instead.');
}

didWarnAboutMaps = true;
} // First, validate keys.
// We'll get a different iterator later for the main pass.


var _newChildren = iteratorFn.call(newChildrenIterable);

if (_newChildren) {
var knownKeys = null;

var _step = _newChildren.next();

for (; !_step.done; _step = _newChildren.next()) {
var child = _step.value;
knownKeys = warnOnInvalidKey(child, knownKeys, returnFiber);
didWarnAboutMaps = true;
}
}
}

var newChildren = iteratorFn.call(newChildrenIterable);

if (newChildren == null) {
throw new Error('An iterable object provided no iterator.');
}
Expand All @@ -7036,9 +7027,14 @@ function createChildReconciler(shouldTrackSideEffects) {
var lastPlacedIndex = 0;
var newIdx = 0;
var nextOldFiber = null;
var knownKeys = null;
var step = newChildren.next();

for (; oldFiber !== null && !step.done; newIdx++, step = newChildren.next()) {
{
knownKeys = warnOnInvalidKey(step.value, knownKeys, returnFiber);
}

for (; oldFiber !== null && !step.done; newIdx++, step = newChildren.next(), knownKeys = warnOnInvalidKey(step.value, knownKeys, returnFiber) ) {
if (oldFiber.index > newIdx) {
nextOldFiber = oldFiber;
oldFiber = null;
Expand Down Expand Up @@ -7095,7 +7091,7 @@ function createChildReconciler(shouldTrackSideEffects) {
if (oldFiber === null) {
// If we don't have any more existing children we can choose a fast path
// since the rest will all be insertions.
for (; !step.done; newIdx++, step = newChildren.next()) {
for (; !step.done; newIdx++, step = newChildren.next(), knownKeys = warnOnInvalidKey(step.value, knownKeys, returnFiber) ) {
var _newFiber3 = createChild(returnFiber, step.value, lanes, debugInfo);

if (_newFiber3 === null) {
Expand All @@ -7120,7 +7116,7 @@ function createChildReconciler(shouldTrackSideEffects) {

var existingChildren = mapRemainingChildren(oldFiber); // Keep scanning and use the map to restore deleted items as moves.

for (; !step.done; newIdx++, step = newChildren.next()) {
for (; !step.done; newIdx++, step = newChildren.next(), knownKeys = warnOnInvalidKey(step.value, knownKeys, returnFiber) ) {
var _newFiber4 = updateFromMap(existingChildren, returnFiber, newIdx, step.value, lanes, debugInfo);

if (_newFiber4 !== null) {
Expand Down
64 changes: 30 additions & 34 deletions compiled/facebook-www/ReactART-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = '19.0.0-www-modern-41846fdd';
var ReactVersion = '19.0.0-www-modern-3c131a55';

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -6776,45 +6776,36 @@ function createChildReconciler(shouldTrackSideEffects) {
throw new Error('An object is not an iterable. This error is likely caused by a bug in ' + 'React. Please file an issue.');
}

{
// We don't support rendering Generators because it's a mutation.
// See https://github.com/facebook/react/issues/12995
if (typeof Symbol === 'function' && // $FlowFixMe[prop-missing] Flow doesn't know about toStringTag
newChildrenIterable[Symbol.toStringTag] === 'Generator') {
if (!didWarnAboutGenerators) {
error('Using Generators as children is unsupported and will likely yield ' + 'unexpected results because enumerating a generator mutates it. ' + 'You may convert it to an array with `Array.from()` or the ' + '`[...spread]` operator before rendering. Keep in mind ' + 'you might need to polyfill these features for older browsers.');
}

didWarnAboutGenerators = true;
} // Warn about using Maps as children
var newChildren = iteratorFn.call(newChildrenIterable);

{
if (newChildren === newChildrenIterable) {
// We don't support rendering Generators as props because it's a mutation.
// See https://github.com/facebook/react/issues/12995
// We do support generators if they were created by a GeneratorFunction component
// as its direct child since we can recreate those by rerendering the component
// as needed.
var isGeneratorComponent = returnFiber.tag === FunctionComponent && // $FlowFixMe[method-unbinding]
Object.prototype.toString.call(returnFiber.type) === '[object GeneratorFunction]' && // $FlowFixMe[method-unbinding]
Object.prototype.toString.call(newChildren) === '[object Generator]';

if (!isGeneratorComponent) {
if (!didWarnAboutGenerators) {
error('Using Iterators as children is unsupported and will likely yield ' + 'unexpected results because enumerating a generator mutates it. ' + 'You may convert it to an array with `Array.from()` or the ' + '`[...spread]` operator before rendering. You can also use an ' + 'Iterable that can iterate multiple times over the same items.');
}

if (newChildrenIterable.entries === iteratorFn) {
didWarnAboutGenerators = true;
}
} else if (newChildrenIterable.entries === iteratorFn) {
// Warn about using Maps as children
if (!didWarnAboutMaps) {
error('Using Maps as children is not supported. ' + 'Use an array of keyed ReactElements instead.');
}

didWarnAboutMaps = true;
} // First, validate keys.
// We'll get a different iterator later for the main pass.


var _newChildren = iteratorFn.call(newChildrenIterable);

if (_newChildren) {
var knownKeys = null;

var _step = _newChildren.next();

for (; !_step.done; _step = _newChildren.next()) {
var child = _step.value;
knownKeys = warnOnInvalidKey(child, knownKeys, returnFiber);
didWarnAboutMaps = true;
}
}
}

var newChildren = iteratorFn.call(newChildrenIterable);

if (newChildren == null) {
throw new Error('An iterable object provided no iterator.');
}
Expand All @@ -6825,9 +6816,14 @@ function createChildReconciler(shouldTrackSideEffects) {
var lastPlacedIndex = 0;
var newIdx = 0;
var nextOldFiber = null;
var knownKeys = null;
var step = newChildren.next();

for (; oldFiber !== null && !step.done; newIdx++, step = newChildren.next()) {
{
knownKeys = warnOnInvalidKey(step.value, knownKeys, returnFiber);
}

for (; oldFiber !== null && !step.done; newIdx++, step = newChildren.next(), knownKeys = warnOnInvalidKey(step.value, knownKeys, returnFiber) ) {
if (oldFiber.index > newIdx) {
nextOldFiber = oldFiber;
oldFiber = null;
Expand Down Expand Up @@ -6884,7 +6880,7 @@ function createChildReconciler(shouldTrackSideEffects) {
if (oldFiber === null) {
// If we don't have any more existing children we can choose a fast path
// since the rest will all be insertions.
for (; !step.done; newIdx++, step = newChildren.next()) {
for (; !step.done; newIdx++, step = newChildren.next(), knownKeys = warnOnInvalidKey(step.value, knownKeys, returnFiber) ) {
var _newFiber3 = createChild(returnFiber, step.value, lanes, debugInfo);

if (_newFiber3 === null) {
Expand All @@ -6909,7 +6905,7 @@ function createChildReconciler(shouldTrackSideEffects) {

var existingChildren = mapRemainingChildren(oldFiber); // Keep scanning and use the map to restore deleted items as moves.

for (; !step.done; newIdx++, step = newChildren.next()) {
for (; !step.done; newIdx++, step = newChildren.next(), knownKeys = warnOnInvalidKey(step.value, knownKeys, returnFiber) ) {
var _newFiber4 = updateFromMap(existingChildren, returnFiber, newIdx, step.value, lanes, debugInfo);

if (_newFiber4 !== null) {
Expand Down
10 changes: 5 additions & 5 deletions compiled/facebook-www/ReactART-prod.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2217,7 +2217,7 @@ function createChildReconciler(shouldTrackSideEffects) {
nextOldFiber = null,
step = newChildrenIterable.next();
null !== oldFiber && !step.done;
newIdx++, step = newChildrenIterable.next()
newIdx++, step = newChildrenIterable.next(), null
) {
oldFiber.index > newIdx
? ((nextOldFiber = oldFiber), (oldFiber = null))
Expand All @@ -2241,7 +2241,7 @@ function createChildReconciler(shouldTrackSideEffects) {
if (step.done)
return deleteRemainingChildren(returnFiber, oldFiber), iteratorFn;
if (null === oldFiber) {
for (; !step.done; newIdx++, step = newChildrenIterable.next())
for (; !step.done; newIdx++, step = newChildrenIterable.next(), null)
(step = createChild(returnFiber, step.value, lanes)),
null !== step &&
((currentFirstChild = placeChild(step, currentFirstChild, newIdx)),
Expand All @@ -2254,7 +2254,7 @@ function createChildReconciler(shouldTrackSideEffects) {
for (
oldFiber = mapRemainingChildren(oldFiber);
!step.done;
newIdx++, step = newChildrenIterable.next()
newIdx++, step = newChildrenIterable.next(), null
)
(step = updateFromMap(oldFiber, returnFiber, newIdx, step.value, lanes)),
null !== step &&
Expand Down Expand Up @@ -10622,7 +10622,7 @@ var slice = Array.prototype.slice,
return null;
},
bundleType: 0,
version: "19.0.0-www-classic-4cca10ad",
version: "19.0.0-www-classic-a0422099",
rendererPackageName: "react-art"
};
var internals$jscomp$inline_1322 = {
Expand Down Expand Up @@ -10653,7 +10653,7 @@ var internals$jscomp$inline_1322 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "19.0.0-www-classic-4cca10ad"
reconcilerVersion: "19.0.0-www-classic-a0422099"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1323 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
Loading

0 comments on commit 91c2cfe

Please sign in to comment.