Skip to content

Commit

Permalink
Warn if accessing .type on a factory
Browse files Browse the repository at this point in the history
This was an important convenience as an upgrade path but shouldn't be
necessary if you're using best-practice of calling createFactory in the
consuming component.
  • Loading branch information
sebmarkbage committed Feb 17, 2015
1 parent 6d97c70 commit 3587460
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/classic/element/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,33 @@ var ReactElementValidator = {
);
// Legacy hook TODO: Warn if this is accessed
validatedFactory.type = type;

if (__DEV__) {
try {
Object.defineProperty(
validatedFactory,
'type',
{
enumerable: false,
get: function() {
warning(
false,
'Factory.type is deprecated. Access the class directly ' +
'before passing it to createFactory.'
);
Object.defineProperty(this, 'type', {
value: type
});
return type;
}
}
);
} catch (x) {
// IE will fail on defineProperty (es5-shim/sham too)
}
}


return validatedFactory;
}

Expand Down
19 changes: 19 additions & 0 deletions src/classic/element/__tests__/ReactElementValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,23 @@ describe('ReactElementValidator', function() {
expect(console.warn.calls[0].args[0]).toContain('use of a keyed object');
});

it('should warn when accessing .type on an element factory', function() {
spyOn(console, 'warn');
var TestComponent = React.createClass({
render: function() {
return <div />;
}
});
var TestFactory = React.createFactory(TestComponent);
expect(TestFactory.type).toBe(TestComponent);
expect(console.warn.argsForCall.length).toBe(1);
expect(console.warn.argsForCall[0][0]).toBe(
'Warning: Factory.type is deprecated. Access the class directly before ' +
'passing it to createFactory.'
);
// Warn once, not again
expect(TestFactory.type).toBe(TestComponent);
expect(console.warn.argsForCall.length).toBe(1);
});

});

0 comments on commit 3587460

Please sign in to comment.