mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Fix 'this' in static methods
binds static methods on the descriptor to the component's actual constructor, so that `foo.constructor.bar()` and `Foo.bar()` run with the same `this`.
This commit is contained in:
committed by
Paul O’Shannessy
parent
431155d2e2
commit
a6cd945d9f
@@ -549,7 +549,9 @@ function mixStaticSpecIntoComponent(ConvenienceConstructor, statics) {
|
||||
);
|
||||
result = createChainedFunction(existingProperty, property);
|
||||
}
|
||||
ConvenienceConstructor[name] = result;
|
||||
ConvenienceConstructor[name] = typeof result === 'function' ?
|
||||
result.bind(ConvenienceConstructor.type) :
|
||||
result;
|
||||
ConvenienceConstructor.type[name] = result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1201,7 +1201,10 @@ describe('ReactCompositeComponent', function() {
|
||||
abc: 'def',
|
||||
def: 0,
|
||||
ghi: null,
|
||||
jkl: 'mno'
|
||||
jkl: 'mno',
|
||||
pqr: function() {
|
||||
return this;
|
||||
}
|
||||
},
|
||||
|
||||
render: function() {
|
||||
@@ -1218,6 +1221,8 @@ describe('ReactCompositeComponent', function() {
|
||||
expect(Component.ghi).toBe(null);
|
||||
expect(instance.constructor.jkl).toBe('mno');
|
||||
expect(Component.jkl).toBe('mno');
|
||||
expect(instance.constructor.pqr()).toBe(Component.type);
|
||||
expect(Component.pqr()).toBe(Component.type);
|
||||
});
|
||||
|
||||
it('should support statics in mixins', function() {
|
||||
|
||||
Reference in New Issue
Block a user