@@ -3,10 +3,14 @@ import { connect } from 'react-redux'
3
3
import { convertInputToLogic , convertPartialDynamicInput , clearLogicCache } from '../logic/index'
4
4
5
5
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
+ }
10
14
11
15
wrapper . _isKeaFunction = true
12
16
wrapper . _isKeaSingleton = ! input . key
@@ -46,6 +50,27 @@ const mapDispatchToPropsCreator = input => (dispatch, ownProps) => {
46
50
}
47
51
}
48
52
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
+
49
74
export function resetKeaLogicCache ( ) {
50
75
clearLogicCache ( )
51
76
}
0 commit comments