Skip to content

Commit 9f6ac1a

Browse files
committed
add this.actions support to react components
1 parent b89b05f commit 9f6ac1a

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/kea/index.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import { connect } from 'react-redux'
33
import { convertInputToLogic, convertPartialDynamicInput, clearLogicCache } from '../logic/index'
44

55
export function kea (input) {
6-
const wrapper = connect(
7-
mapStateToPropsCreator(input),
8-
mapDispatchToPropsCreator(input)
9-
)
6+
const wrapper = (Klass) => {
7+
injectActionsIntoClass(Klass)
8+
9+
return connect(
10+
mapStateToPropsCreator(input),
11+
mapDispatchToPropsCreator(input)
12+
)(Klass)
13+
}
1014

1115
wrapper._isKeaFunction = true
1216
wrapper._isKeaSingleton = !input.key
@@ -46,6 +50,27 @@ const mapDispatchToPropsCreator = input => (dispatch, ownProps) => {
4650
}
4751
}
4852

53+
function isStateless (Component) {
54+
return (
55+
typeof Component === 'function' && // can be various things
56+
!(Component.prototype && Component.prototype.isReactComponent) // native arrows don't have prototypes // special property
57+
)
58+
}
59+
60+
function injectActionsIntoClass (Klass) {
61+
if (!isStateless(Klass)) {
62+
// inject to the component something that
63+
// converts this.props.actions to this.actions
64+
if (!Object.getOwnPropertyDescriptor(Klass.prototype, 'actions')) {
65+
Object.defineProperty(Klass.prototype, 'actions', {
66+
get: function actions () {
67+
return this.props.actions
68+
}
69+
})
70+
}
71+
}
72+
}
73+
4974
export function resetKeaLogicCache () {
5075
clearLogicCache()
5176
}

0 commit comments

Comments
 (0)