Skip to content

Commit

Permalink
Warn about rendering Generators
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Aug 2, 2018
1 parent 46d5afc commit 181125a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/react-dom/src/__tests__/ReactMultiChild-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,27 @@ describe('ReactMultiChild', () => {
);
});

it('should warn for using generators as children', () => {
function* Foo() {
yield <h1 key="1">Hello</h1>;
yield <h1 key="2">World</h1>;
}

const div = document.createElement('div');
expect(() => {
ReactDOM.render(<Foo />, div);
}).toWarnDev(
'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.\n' +
' in Foo (at **)',
);

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

it('should reorder bailed-out children', () => {
class LetterInner extends React.Component {
render() {
Expand Down
19 changes: 19 additions & 0 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ import {
import {StrictMode} from './ReactTypeOfMode';

let didWarnAboutMaps;
let didWarnAboutGenerators;
let didWarnAboutStringRefInStrictMode;
let ownerHasKeyUseWarning;
let ownerHasFunctionTypeWarning;
let warnForMissingKey = (child: mixed) => {};

if (__DEV__) {
didWarnAboutMaps = false;
didWarnAboutGenerators = false;
didWarnAboutStringRefInStrictMode = {};

/**
Expand Down Expand Up @@ -903,6 +905,23 @@ function ChildReconciler(shouldTrackSideEffects) {
);

if (__DEV__) {
// We don't support rendering Generators because it's a mutation.
// See https://github.com/facebook/react/issues/12995
if (
typeof Symbol === 'function' &&
newChildrenIterable[Symbol.toStringTag] === 'Generator'
) {
warning(
didWarnAboutGenerators,
'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
if ((newChildrenIterable: any).entries === iteratorFn) {
warning(
Expand Down

0 comments on commit 181125a

Please sign in to comment.