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

Added check to deduplicate function type warning calls on each compon… #11120

Merged
merged 4 commits into from
Oct 6, 2017
Merged
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
37 changes: 37 additions & 0 deletions src/renderers/__tests__/ReactComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,5 +542,42 @@ describe('ReactComponent', () => {
expect(container.innerHTML).toBe('Hello');
expectDev(console.error.calls.count()).toBe(0);
});

it('deduplicates function type warnings based on component type', () => {
spyOn(console, 'error');
class Foo extends React.PureComponent {
constructor() {
super();
this.state = {type: 'mushrooms'};
}
render() {
return (
<div>
{Foo}{Foo}
<span>{Foo}{Foo}</span>
</div>
);
}
}
var container = document.createElement('div');
var component = ReactDOM.render(<Foo />, container);
component.setState({type: 'portobello mushrooms'});
expectDev(console.error.calls.count()).toBe(2);
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 div (at **)\n' +
' in Foo (at **)',
);
expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(1)[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 **)',
);
});
});
});
12 changes: 12 additions & 0 deletions src/renderers/shared/fiber/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ if (__DEV__) {
* updates.
*/
var ownerHasKeyUseWarning = {};
var ownerHasFunctionTypeWarning = {};

var warnForMissingKey = (child: mixed) => {
if (child === null || typeof child !== 'object') {
Expand Down Expand Up @@ -192,6 +193,17 @@ function throwOnInvalidObjectType(returnFiber: Fiber, newChild: Object) {
}

function warnOnFunctionType() {
const currentComponentErrorInfo =
'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.' +
(getCurrentFiberStackAddendum() || '');

if (ownerHasFunctionTypeWarning[currentComponentErrorInfo]) {
return;
}
ownerHasFunctionTypeWarning[currentComponentErrorInfo] = true;

warning(
false,
'Functions are not valid as a React child. This may happen if ' +
Expand Down