From a6cd945d9fb37915933c2cd7c87477305d429aed Mon Sep 17 00:00:00 2001 From: Marshall Roch Date: Sat, 14 Jun 2014 20:15:29 -0700 Subject: [PATCH] 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`. --- src/core/ReactCompositeComponent.js | 4 +++- src/core/__tests__/ReactCompositeComponent-test.js | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index d31b5c9fb4..2a21a60e39 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -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; } } diff --git a/src/core/__tests__/ReactCompositeComponent-test.js b/src/core/__tests__/ReactCompositeComponent-test.js index 5e0dbe71b5..bd9142a887 100644 --- a/src/core/__tests__/ReactCompositeComponent-test.js +++ b/src/core/__tests__/ReactCompositeComponent-test.js @@ -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() {