@samitouri / QOS-React-2 / commits / 1ed1d7a158

Add warning when accessing .type on a component class

Since we removed the wrapper factory around classes, the class is just the class now so there is no need for this indirection property.

Sebastian Markbage committed Jan 22, 2015 at 13:26 UTC 1ed1d7a158c1374bdc8c6f1e1c738408a122349c
2 files changed +36 -1
src/classic/class/ReactClass.js
+21 -1
@@ -23,6 +23,7 @@ var invariant = require('invariant');
23 var keyMirror = require('keyMirror');
24 var keyOf = require('keyOf');
25 var monitorCodeUse = require('monitorCodeUse');
26 +var warning = require('warning');
27
28 var MIXINS_KEY = keyOf({mixins: null});
29
@@ -693,6 +694,20 @@ function bindAutoBindMethods(component) {
694 }
695 }
696
697 +var typeDeprecationDescriptor = {
698 + enumerable: false,
699 + get: function() {
700 + var displayName = this.displayName || this.name || 'Component';
701 + warning(
702 + false,
703 + '%s.type is deprecated. Use %s directly to access the class.',
704 + displayName,
705 + displayName
706 + );
707 + return this;
708 + }
709 +};
710 +
711 /**
712 * Add more to the ReactClass base class. These are all legacy features and
713 * therefore not already part of the modern ReactComponentBase.
@@ -877,8 +892,13 @@ var ReactClass = {
892 }
893 }
894
880 - // Legacy hook TODO: Warn if this is accessed
895 + // Legacy hook
896 Constructor.type = Constructor;
897 + if (__DEV__) {
898 + if (Object.defineProperty) {
899 + Object.defineProperty(Constructor, 'type', typeDeprecationDescriptor);
900 + }
901 + }
902
903 return Constructor;
904 },
src/classic/class/__tests__/ReactClass-test.js
+15
@@ -43,6 +43,21 @@ describe('ReactClass-spec', function() {
43 .toBe('TestComponent');
44 });
45
46 + it('should warn when accessing .type on a React class', function() {
47 + var TestComponent = React.createClass({
48 + render: function() {
49 + return <div />;
50 + }
51 + });
52 + console.warn = mocks.getMockFunction();
53 + expect(TestComponent.type).toBe(TestComponent);
54 + expect(console.warn.mock.calls.length).toBe(1);
55 + expect(console.warn.mock.calls[0][0]).toBe(
56 + 'Warning: TestComponent.type is deprecated. Use TestComponent ' +
57 + 'directly to access the class.'
58 + );
59 + });
60 +
61 it('should copy prop types onto the Constructor', function() {
62 var propValidator = mocks.getMockFunction();
63 var TestComponent = React.createClass({