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 about unresolved function as a child #10376

Merged
merged 2 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
78 changes: 76 additions & 2 deletions src/renderers/__tests__/ReactComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
var React;
var ReactDOM;
var ReactDOMServer;
var ReactDOMFeatureFlags;
var ReactTestUtils;

var ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');

describe('ReactComponent', () => {
function normalizeCodeLocInfo(str) {
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
Expand All @@ -26,7 +27,6 @@ describe('ReactComponent', () => {
React = require('react');
ReactDOM = require('react-dom');
ReactDOMServer = require('react-dom/server');
ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
ReactTestUtils = require('react-dom/test-utils');
});

Expand Down Expand Up @@ -493,4 +493,78 @@ describe('ReactComponent', () => {
' in Foo (at **)',
);
});

if (ReactDOMFeatureFlags.useFiber) {
fdescribe('with new features', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-f

beforeEach(() => {
require('ReactFeatureFlags').disableNewFiberFeatures = false;
});

it('warns on function as a return value from a function', () => {
function Foo() {
return Foo;
}
spyOn(console, 'error');
var container = document.createElement('div');
ReactDOM.render(<Foo />, container);
expectDev(console.error.calls.count()).toBe(1);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Functions are not valid as a React child. This may happen if ' +
'you return a Component instead of <Component /> from render. ' +
'Or maybe you meant to call this function rather than return it.\n' +
' in Foo (at **)',
);
});

it('warns on function as a return value from a class', () => {
class Foo extends React.Component {
render() {
return Foo;
}
}
spyOn(console, 'error');
var container = document.createElement('div');
ReactDOM.render(<Foo />, container);
expectDev(console.error.calls.count()).toBe(1);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Functions are not valid as a React child. This may happen if ' +
'you return a Component instead of <Component /> from render. ' +
'Or maybe you meant to call this function rather than return it.\n' +
' in Foo (at **)',
);
});

it('warns on function as a child to host component', () => {
function Foo() {
return <div><span>{Foo}</span></div>;
}
spyOn(console, 'error');
var container = document.createElement('div');
ReactDOM.render(<Foo />, container);
expectDev(console.error.calls.count()).toBe(1);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Functions are not valid as a React child. This may happen if ' +
'you return a Component instead of <Component /> from render. ' +
'Or maybe you meant to call this function rather than return it.\n' +
' in span (at **)\n' +
' in div (at **)\n' +
' in Foo (at **)',
);
});

it('does not warn for function-as-a-child that gets resolved', () => {
function Bar(props) {
return props.children();
}
function Foo() {
return <Bar>{() => 'Hello'}</Bar>;
}
spyOn(console, 'error');
var container = document.createElement('div');
ReactDOM.render(<Foo />, container);
expect(container.innerHTML).toBe('Hello');
expectDev(console.error.calls.count()).toBe(0);
});
});
}
});
36 changes: 36 additions & 0 deletions src/renderers/shared/fiber/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ function throwOnInvalidObjectType(returnFiber: Fiber, newChild: Object) {
}
}

function warnOnFunctionType() {
warning(
false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not pass in newChild and put typeof newChild !== 'function' instead of false?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid two extra functions calls in DEV. (It's small but can add up.)

Also I find our warning notation with falsy condition meaning a failure very confusing. I try to avoid it whenever I write warnings, and always use warning(false so it’s clear from outer condition in which case it executes.

'Functions are not valid as a React child. This may happen if ' +
'you return a Component instead of <Component /> from render. ' +
'Or maybe you meant to call this function rather than return it.%s',
getCurrentFiberStackAddendum() || '',
);
}

// This wrapper function exists because I expect to clone the code in each path
// to be able to optimize each path individually by branching early. This needs
// a compiler or we can do it manually. Helpers that don't need this branching
Expand Down Expand Up @@ -565,6 +575,13 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) {
throwOnInvalidObjectType(returnFiber, newChild);
}

if (__DEV__) {
const disableNewFiberFeatures = ReactFeatureFlags.disableNewFiberFeatures;
if (!disableNewFiberFeatures && typeof newChild === 'function') {
warnOnFunctionType();
}
}

return null;
}

Expand Down Expand Up @@ -638,6 +655,13 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) {
throwOnInvalidObjectType(returnFiber, newChild);
}

if (__DEV__) {
const disableNewFiberFeatures = ReactFeatureFlags.disableNewFiberFeatures;
if (!disableNewFiberFeatures && typeof newChild === 'function') {
warnOnFunctionType();
}
}

return null;
}

Expand Down Expand Up @@ -697,6 +721,13 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) {
throwOnInvalidObjectType(returnFiber, newChild);
}

if (__DEV__) {
const disableNewFiberFeatures = ReactFeatureFlags.disableNewFiberFeatures;
if (!disableNewFiberFeatures && typeof newChild === 'function') {
warnOnFunctionType();
}
}

return null;
}

Expand Down Expand Up @@ -1418,6 +1449,11 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) {
throwOnInvalidObjectType(returnFiber, newChild);
}

if (__DEV__) {
if (!disableNewFiberFeatures && typeof newChild === 'function') {
warnOnFunctionType();
}
}
if (!disableNewFiberFeatures && typeof newChild === 'undefined') {
// If the new child is undefined, and the return fiber is a composite
// component, throw an error. If Fiber return types are disabled,
Expand Down