Skip to content

Commit a117e30

Browse files
committed
Allow connectToStores use without static methods
1 parent 841979e commit a117e30

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/utils/connectToStores.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,27 @@
4646
import React from 'react'
4747
import { assign, isFunction } from './functions'
4848

49-
function connectToStores(Component) {
49+
function connectToStores(Spec, Component = Spec) {
5050
// Check for required static methods.
51-
if (!isFunction(Component.getStores)) {
51+
if (!isFunction(Spec.getStores)) {
5252
throw new Error('connectToStores() expects the wrapped component to have a static getStores() method')
5353
}
54-
if (!isFunction(Component.getPropsFromStores)) {
54+
if (!isFunction(Spec.getPropsFromStores)) {
5555
throw new Error('connectToStores() expects the wrapped component to have a static getPropsFromStores() method')
5656
}
5757

58-
// Wrapper Component.
5958
const StoreConnection = React.createClass({
6059
getInitialState() {
61-
return Component.getPropsFromStores(this.props, this.context)
60+
return Spec.getPropsFromStores(this.props, this.context)
6261
},
6362

6463
componentDidMount() {
65-
const stores = Component.getStores(this.props, this.context)
64+
const stores = Spec.getStores(this.props, this.context)
6665
this.storeListeners = stores.map((store) => {
6766
return store.listen(this.onChange)
6867
})
69-
if (Component.componentDidConnect) {
70-
Component.componentDidConnect(this.props, this.context)
68+
if (Spec.componentDidConnect) {
69+
Spec.componentDidConnect(this.props, this.context)
7170
}
7271
},
7372

@@ -76,7 +75,7 @@ function connectToStores(Component) {
7675
},
7776

7877
onChange() {
79-
this.setState(Component.getPropsFromStores(this.props, this.context))
78+
this.setState(Spec.getPropsFromStores(this.props, this.context))
8079
},
8180

8281
render() {

test/connect-to-stores-test.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,19 @@ export default {
132132

133133
'ES6 class component responds to store events'() {
134134
class ClassComponent extends React.Component {
135-
static getStores() {
136-
return [testStore]
137-
}
138-
static getPropsFromStores(props) {
139-
return testStore.getState()
140-
}
141135
render() {
142136
return <span foo={this.props.foo} />
143137
}
144138
}
145139

146-
const WrappedComponent = connectToStores(ClassComponent)
140+
const WrappedComponent = connectToStores({
141+
getStores() {
142+
return [testStore]
143+
},
144+
getPropsFromStores(props) {
145+
return testStore.getState()
146+
}
147+
}, ClassComponent)
147148

148149
const node = TestUtils.renderIntoDocument(
149150
<WrappedComponent />
@@ -159,25 +160,27 @@ export default {
159160
'componentDidConnect hook is called '() {
160161
let componentDidConnect = false
161162
class ClassComponent extends React.Component {
162-
static getStores() {
163-
return [testStore]
164-
}
165-
static getPropsFromStores(props) {
166-
return testStore.getState()
167-
}
168-
static componentDidConnect() {
169-
componentDidConnect = true
170-
}
171163
render() {
172164
return <span foo={this.props.foo} />
173165
}
174166
}
175-
const WrappedComponent = connectToStores(ClassComponent)
167+
const WrappedComponent = connectToStores({
168+
getStores() {
169+
return [testStore]
170+
},
171+
getPropsFromStores(props) {
172+
return testStore.getState()
173+
},
174+
componentDidConnect() {
175+
componentDidConnect = true
176+
}
177+
}, ClassComponent)
176178
const node = TestUtils.renderIntoDocument(
177179
<WrappedComponent />
178180
)
179181
assert(componentDidConnect === true)
180182
},
183+
181184
'Component receives all updates'(done) {
182185
let componentDidConnect = false
183186
class ClassComponent extends React.Component {

0 commit comments

Comments
 (0)