File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change 10
10
*
11
11
* const MyComponent = React.createClass({
12
12
* statics: {
13
- * getStores() {
13
+ * getStores(props ) {
14
14
* return [myStore]
15
15
* },
16
16
* getPropsFromStores(props) {
27
27
* Example using ES6 Class:
28
28
*
29
29
* class MyComponent extends React.Component {
30
- * static getStores() {
30
+ * static getStores(props ) {
31
31
* return [myStore]
32
32
* }
33
33
* static getPropsFromStores(props) {
@@ -55,22 +55,21 @@ function connectToStores(Component) {
55
55
throw new Error ( 'connectToStores() expects the wrapped component to have a static getPropsFromStores() method' )
56
56
}
57
57
58
- // Cache stores.
59
- const stores = Component . getStores ( )
60
-
61
58
// Wrapper Component.
62
59
const StoreConnection = React . createClass ( {
63
60
getInitialState ( ) {
64
61
return Component . getPropsFromStores ( this . props )
65
62
} ,
66
63
67
64
componentDidMount ( ) {
65
+ const stores = Component . getStores ( this . props )
68
66
stores . forEach ( ( store ) => {
69
67
store . listen ( this . onChange )
70
68
} )
71
69
} ,
72
70
73
71
componentWillUnmount ( ) {
72
+ const stores = Component . getStores ( this . props )
74
73
stores . forEach ( ( store ) => {
75
74
store . unlisten ( this . onChange )
76
75
} )
Original file line number Diff line number Diff line change @@ -109,6 +109,27 @@ export default {
109
109
assert . include ( output , 'Foo: Bar' )
110
110
} ,
111
111
112
+ 'component can get use stores from props' ( ) {
113
+ const LegacyComponent = React . createClass ( {
114
+ statics : {
115
+ getStores ( props ) {
116
+ return [ props . store ]
117
+ } ,
118
+ getPropsFromStores ( props ) {
119
+ return props . store . getState ( )
120
+ }
121
+ } ,
122
+ render ( ) {
123
+ return React . createElement ( 'div' , null , `Foo${ this . props . delim } ${ this . props . foo } ` )
124
+ }
125
+ } )
126
+
127
+ const WrappedComponent = connectToStores ( LegacyComponent )
128
+ const element = React . createElement ( WrappedComponent , { delim : ': ' , store : testStore } )
129
+ const output = React . renderToStaticMarkup ( element )
130
+ assert . include ( output , 'Foo: Bar' )
131
+ } ,
132
+
112
133
'ES6 class component responds to store events' ( ) {
113
134
class ClassComponent extends React . Component {
114
135
static getStores ( ) {
You can’t perform that action at this time.
0 commit comments