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

Constructor error message #11395

Merged
merged 9 commits into from
Nov 1, 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
22 changes: 16 additions & 6 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,22 @@ module.exports = function(
if (__DEV__) {
const name = getComponentName(workInProgress);
const renderPresent = instance.render;
warning(
renderPresent,
'%s(...): No `render` method found on the returned component ' +
'instance: you may have forgotten to define `render`.',
name,
);

if (type.prototype && type.prototype.render !== undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's put the whole thing into if (!renderPresent) so we don't run those checks unnecessarily. And make it warning(false.

warning(
renderPresent,
'%s(...): No `render` method found on the returned component ' +
'instance: is the `constructor` defined well?',
name,
);
} else {
warning(
renderPresent,
'%s(...): No `render` method found on the returned component ' +
'instance: you may have forgotten to define `render`.',
name,
);
}
const noGetInitialStateOnES6 =
!instance.getInitialState ||
instance.getInitialState.isReactClassApproved ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
describe('ReactFiberClassComponent', () => {
let React;
let ReactNoop;

beforeEach(() => {
jest.resetModules();
React = require('react');
ReactNoop = require('react-noop-renderer');
});

it('should render a class if nominal', () => {
class NominalComponent extends React.Component {
render() {
return <div />;
}
}

ReactNoop.render(<NominalComponent />);
ReactNoop.flush();
});

it('should return a meaningful warning when constructor is returned', () => {
spyOn(console, 'error');
class RenderTextInvalidConstructor extends React.Component {
constructor(props) {
super(props);
return {something: false};
}

render() {
return <div />;
}
}

ReactNoop.render(<RenderTextInvalidConstructor />);
ReactNoop.flushUnitsOfWork(2);

const error = console.error.calls.mostRecent().args[0];

expectDev(error).toBe(
'Warning: RenderTextInvalidConstructor(...): No `render` method found on the returned ' +
'component instance: is the `constructor` defined well?',
);
});

it('should return error if render is not defined', () => {
spyOn(console, 'error');
class RenderTestUndefinedRender extends React.Component {}

ReactNoop.render(<RenderTestUndefinedRender />);
ReactNoop.flushUnitsOfWork(2);

const error = console.error.calls.mostRecent().args[0];

expectDev(error).toBe(
'Warning: RenderTestUndefinedRender(...): No `render` method found on the returned ' +
'component instance: you may have forgotten to define `render`.',
);
});
});