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 if class has a render() method but doesn't extend React.Component #11168

Merged
merged 4 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions packages/react-dom/src/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,32 @@ describe('ReactCompositeComponent', () => {
expect(cbCalled).toBe(false);
});

it(
'should throw an Error with a warning when rendering' +
"a class with a render method that doesn't extend React.Component",
() => {
spyOn(console, 'error');
var container = document.createElement('div');
class ClassWithRenderNotExtended {
render() {
return <div />;
}
}
expectDev(console.error.calls.count()).toBe(0);
try {
ReactDOM.render(<ClassWithRenderNotExtended />, container);
} catch (e) {
expect(e).toEqual(new TypeError('Cannot call a class as a function'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This only runs assertions if we get into catch. But what if we have a bug that causes us not to throw an error? Then the test doesn't assert anything.

expectDev(console.error.calls.count()).toBe(1);
expectDev(console.error.calls.argsFor(0)[0]).toContain(
'Warning: The <ClassWithRenderNotExtended /> component appears to have a render method, ' +
"but doesn't extend React.Component. This is likely to cause errors. " +
'Change ClassWithRenderNotExtended to extend React.Component instead.',
);
}
},
);

it('should warn about `setState` in render', () => {
spyOn(console, 'error');

Expand Down
12 changes: 12 additions & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var {
} = require('shared/ReactTypeOfSideEffect');
var {ReactCurrentOwner} = require('shared/ReactGlobalSharedState');
var invariant = require('fbjs/lib/invariant');
var getComponentName = require('shared/getComponentName');

var ReactFiberClassComponent = require('./ReactFiberClassComponent');
var {
Expand Down Expand Up @@ -471,6 +472,17 @@ module.exports = function<T, P, I, TI, PI, C, CC, CX, PL>(
var value;

if (__DEV__) {
const classWithRenderButNotExtendedFromReactComponent =
workInProgress.type.prototype && workInProgress.type.prototype.render;
if (classWithRenderButNotExtendedFromReactComponent) {
warning(
false,
"The <%s /> component appears to have a render method, but doesn't extend React.Component. " +
'This is likely to cause errors. Change %s to extend React.Component instead.',
getComponentName(workInProgress),
getComponentName(workInProgress),
);
}
ReactCurrentOwner.current = workInProgress;
value = fn(props, context);
} else {
Expand Down