Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when using one-shot iterators as children #24470

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/react-dom/src/__tests__/ReactMultiChild-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,29 @@ describe('ReactMultiChild', () => {
ReactDOM.render(<Foo />, div);
});

it('should warn when using one-shot iterators as children', () => {
function Foo() {
const iterator = [<h1 key="1">Hello</h1>, <h1 key="2">Dave</h1>].values();

return iterator;
}

const div = document.createElement('div');
expect(() => {
ReactDOM.render(<Foo />, div);
}).toErrorDev(
'Warning: Using an iterator such as `[].values()` as children is unsupported and will likely yield ' +
'unexpected results because enumerating such an iterator mutates it. ' +
'You may convert it to an array with `Array.from()` or the ' +
'`[...spread]` operator before rendering. Keep in mind that ' +
'you might need to polyfill these features for older browsers.\n' +
' in Foo (at **)',
);

// Test de-duplication
ReactDOM.render(<Foo />, div);
});

it('should not warn for using generators in legacy iterables', () => {
const fooIterable = {
'@@iterator': function*() {
Expand Down
17 changes: 17 additions & 0 deletions packages/react-reconciler/src/ReactChildFiber.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {pushTreeFork} from './ReactFiberTreeContext.new';

let didWarnAboutMaps;
let didWarnAboutGenerators;
let didWarnAboutOneShotIterators;
let didWarnAboutStringRefs;
let ownerHasKeyUseWarning;
let ownerHasFunctionTypeWarning;
Expand Down Expand Up @@ -942,6 +943,22 @@ function ChildReconciler(shouldTrackSideEffects) {
);
}
didWarnAboutGenerators = true;
} else {
// Warn about using one-shot iterators as children
const isOneShotIterator =
iteratorFn.call(newChildrenIterable) === newChildrenIterable;
if (isOneShotIterator) {
if (!didWarnAboutOneShotIterators) {
console.error(
'Using an iterator such as `[].values()` as children is unsupported and will likely yield ' +
'unexpected results because enumerating such an iterator mutates it. ' +
'You may convert it to an array with `Array.from()` or the ' +
'`[...spread]` operator before rendering. Keep in mind that ' +
'you might need to polyfill these features for older browsers.',
);
}
didWarnAboutOneShotIterators = true;
}
}

// Warn about using Maps as children
Expand Down
17 changes: 17 additions & 0 deletions packages/react-reconciler/src/ReactChildFiber.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {pushTreeFork} from './ReactFiberTreeContext.old';

let didWarnAboutMaps;
let didWarnAboutGenerators;
let didWarnAboutOneShotIterators;
let didWarnAboutStringRefs;
let ownerHasKeyUseWarning;
let ownerHasFunctionTypeWarning;
Expand Down Expand Up @@ -942,6 +943,22 @@ function ChildReconciler(shouldTrackSideEffects) {
);
}
didWarnAboutGenerators = true;
} else {
// Warn about using one-shot iterators as children
const isOneShotIterator =
iteratorFn.call(newChildrenIterable) === newChildrenIterable;
if (isOneShotIterator) {
if (!didWarnAboutOneShotIterators) {
console.error(
'Using an iterator such as `[].values()` as children is unsupported and will likely yield ' +
'unexpected results because enumerating such an iterator mutates it. ' +
'You may convert it to an array with `Array.from()` or the ' +
'`[...spread]` operator before rendering. Keep in mind that ' +
'you might need to polyfill these features for older browsers.',
);
}
didWarnAboutOneShotIterators = true;
}
}

// Warn about using Maps as children
Expand Down