Warn when using constructor function directly

We no longer support the legacy factory style of calling component constructors
directly. We only support createElement or the wrapping of classes with
createFactory. Instead of letting this fail in a gross way as we try to run,
add a nice warning that shows up before the gross TypeError.
This commit is contained in:
Paul O’Shannessy
2015-02-19 13:57:48 -08:00
parent a2f77208e6
commit ad31cfa4dc
2 changed files with 23 additions and 0 deletions
+8
View File
@@ -828,6 +828,14 @@ var ReactClass = {
// This constructor is overridden by mocks. The argument is used
// by mocks to assert on what gets mounted.
if (__DEV__) {
warning(
this instanceof Constructor,
'Something is calling a React component directly. Use a factory or ' +
'JSX instead. See: http://fb.me/react-legacyfactory'
);
}
// Wire up auto-binding
if (this.__reactAutoBindMap) {
bindAutoBindMethods(this);
@@ -363,4 +363,19 @@ describe('ReactClass-spec', function() {
).not.toThrow();
});
it('should throw when using legacy factories', function() {
var Component = React.createClass({
render() {
return <div />;
}
});
expect(() => Component()).toThrow();
expect(console.warn.calls.length).toBe(1);
expect(console.warn.argsForCall[0][0]).toBe(
'Warning: Something is calling a React component directly. Use a ' +
'factory or JSX instead. See: http://fb.me/react-legacyfactory'
);
});
});