From 628928e3108df9725f07689e3785b5a2a226baa8 Mon Sep 17 00:00:00 2001 From: Parakleta Date: Fri, 6 Oct 2017 12:22:50 +1100 Subject: [PATCH] Make bindActionCreators transparently pass `this`. (#2641) --- src/bindActionCreators.js | 2 +- test/bindActionCreators.spec.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/bindActionCreators.js b/src/bindActionCreators.js index 9b0cc061eb..da650d3b57 100644 --- a/src/bindActionCreators.js +++ b/src/bindActionCreators.js @@ -1,5 +1,5 @@ function bindActionCreator(actionCreator, dispatch) { - return (...args) => dispatch(actionCreator(...args)) + return function() { return dispatch(actionCreator.apply(this, arguments)) } } /** diff --git a/test/bindActionCreators.spec.js b/test/bindActionCreators.spec.js index f4a8cb2b44..7c8a517c96 100644 --- a/test/bindActionCreators.spec.js +++ b/test/bindActionCreators.spec.js @@ -33,6 +33,21 @@ describe('bindActionCreators', () => { ]) }) + it('wraps action creators transparently', () => { + const uniqueThis = {} + const argArray = [1, 2, 3] + function actionCreator() { + return { type: 'UNKNOWN_ACTION', this: this, args: [...arguments] } + } + const boundActionCreator = bindActionCreators(actionCreator, store.dispatch) + + const boundAction = boundActionCreator.apply(uniqueThis,argArray) + const action = actionCreator.apply(uniqueThis,argArray) + expect(boundAction).toEqual(action) + expect(boundAction.this).toBe(uniqueThis) + expect(action.this).toBe(uniqueThis) + }) + it('skips non-function values in the passed object', () => { const boundActionCreators = bindActionCreators({ ...actionCreators,