Skip to content
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
20 changes: 20 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,26 @@ describe('ReactCompositeComponent', () => {
expect(cbCalled).toBe(false);
});

it('should warn when rendering a class with a render method that does not extend React.Component', () => {
spyOn(console, 'error');
var container = document.createElement('div');
class ClassWithRenderNotExtended {
render() {
return <div />;
}
}
expectDev(console.error.calls.count()).toBe(0);
expect(() => {
ReactDOM.render(<ClassWithRenderNotExtended />, container);
}).toThrow(TypeError);
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
11 changes: 11 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,16 @@ module.exports = function<T, P, I, TI, PI, C, CC, CX, PL>(
var value;

if (__DEV__) {
if (fn.prototype && typeof fn.prototype.render === 'function') {
const componentName = getComponentName(workInProgress);
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.',
componentName,
componentName,
);
}
ReactCurrentOwner.current = workInProgress;
value = fn(props, context);
} else {
Expand Down