Skip to content

Commit 7003703

Browse files
committed
Allow connectToStore components to use props in getStores
1 parent 3ac1f75 commit 7003703

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/utils/connectToStores.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* const MyComponent = React.createClass({
1212
* statics: {
13-
* getStores() {
13+
* getStores(props) {
1414
* return [myStore]
1515
* },
1616
* getPropsFromStores(props) {
@@ -27,7 +27,7 @@
2727
* Example using ES6 Class:
2828
*
2929
* class MyComponent extends React.Component {
30-
* static getStores() {
30+
* static getStores(props) {
3131
* return [myStore]
3232
* }
3333
* static getPropsFromStores(props) {
@@ -55,22 +55,21 @@ function connectToStores(Component) {
5555
throw new Error('connectToStores() expects the wrapped component to have a static getPropsFromStores() method')
5656
}
5757

58-
// Cache stores.
59-
const stores = Component.getStores()
60-
6158
// Wrapper Component.
6259
const StoreConnection = React.createClass({
6360
getInitialState() {
6461
return Component.getPropsFromStores(this.props)
6562
},
6663

6764
componentDidMount() {
65+
const stores = Component.getStores(this.props)
6866
stores.forEach((store) => {
6967
store.listen(this.onChange)
7068
})
7169
},
7270

7371
componentWillUnmount() {
72+
const stores = Component.getStores(this.props)
7473
stores.forEach((store) => {
7574
store.unlisten(this.onChange)
7675
})

test/connect-to-stores-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ export default {
109109
assert.include(output, 'Foo: Bar')
110110
},
111111

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+
112133
'ES6 class component responds to store events'() {
113134
class ClassComponent extends React.Component {
114135
static getStores() {

0 commit comments

Comments
 (0)