Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRef() method to connectToStores to expose wrapped component #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/connectToStores.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ function connectToStores(Spec, Component = Spec) {
this.storeListeners.forEach(unlisten => unlisten())
},

getRef() {
return this.refs.component
},

onChange() {
this.setState(Spec.getPropsFromStores(this.props, this.context))
storeDidChange(this.state)
Expand All @@ -99,7 +103,7 @@ function connectToStores(Spec, Component = Spec) {
render() {
return React.createElement(
Component,
assign({}, this.props, this.state)
assign({ ref: 'component' }, this.props, this.state)
)
},
})
Expand Down
26 changes: 26 additions & 0 deletions test/connect-to-stores-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,32 @@ export default {
assert.deepEqual(storeDidChange, {foo: 'Baz'})
},

'Component ref can be accessed from WrappedComponent'() {
class ClassComponent5 extends React.Component {
static getStores() {
return [testStore]
}
static getPropsFromStores(props) {
return testStore.getState()
}
isAccessible() {
return true
}
render() {
return <span foo={this.props.foo} />
}
}

const WrappedComponent = connectToStores(ClassComponent5)

let node = TestUtils.renderIntoDocument(
<WrappedComponent />
)

const child = node.getRef()
assert(child.isAccessible() === true)
},

'Component receives all updates'(done) {
let componentDidConnect = false
class ClassComponent3 extends React.Component {
Expand Down