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

use setState instead of forceUpdate; use React 0.14 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions source.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@ export default function makeUniflowComponent (Component, stores) {
return class UniflowComponent extends React.Component {
constructor () {
super()
this.forceUpdate = this.forceUpdate.bind(this)
var storeProps = {}
Object.keys(stores).forEach(key => storeProps[key] = stores[key].state)
this.state = { storeProps }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just this.state = storeProps?

this.updateStorePropsState = this.updateStorePropsState.bind(this)
}

updateStorePropsState () {
var storeProps = {}
Object.keys(stores).forEach(key => storeProps[key] = stores[key].state)
this.setState({ storeProps })
}

componentDidMount () {
Object.keys(stores).forEach(key => {
stores[key].addListener('change', this.forceUpdate)
stores[key].addListener('change', this.updateStorePropsState)
})
}

componentWillUnmount () {
Object.keys(stores).forEach(key => {
stores[key].removeListener('change', this.forceUpdate)
stores[key].removeListener('change', this.updateStorePropsState)
})
}

render () {
var storeProps = {}
Object.keys(stores).forEach(key => storeProps[key] = stores[key].state)
return <Component {...storeProps} {...this.props}/>
return <Component {...this.state.storeProps} {...this.props}/>
}
}
}
15 changes: 8 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import should from 'should'
import sinon from 'sinon'
import React from 'react'
import ReactDOM from 'react-dom'
import EventEmitter from 'eventemitter3'
import jsdom from 'mocha-jsdom'
import make from './source'
Expand All @@ -15,7 +16,7 @@ function makeMockStore () {
return store
}

class TestComponent {
class TestComponent extends React.Component {
render () {
return <div/>
}
Expand All @@ -41,21 +42,21 @@ describe('makeUniflowComponent()', () => {

it('subscribes to all props after mounting', () => {
let Component = make(TestComponent, {a: storeA, b: storeB})
React.render(<Component/>, domNode)
ReactDOM.render(<Component/>, domNode)
sinon.assert.called(storeA.addListener)
sinon.assert.called(storeB.addListener)
})

it('throws when rendered with non-store props', () => {
let Component = make(TestComponent, {a: 'foo'})
let fn = () => React.render(<Component/>, domNode)
let fn = () => ReactDOM.render(<Component/>, domNode)
fn.should.throw()
})

it('unsubscribes to all props when unmounting', () => {
let Component = make(TestComponent, {a: storeA, b: storeB})
React.render(<Component/>, domNode)
React.render(<div/>, domNode)
ReactDOM.render(<Component/>, domNode)
ReactDOM.render(<div/>, domNode)
sinon.assert.called(storeA.removeListener)
sinon.assert.called(storeB.removeListener)
})
Expand All @@ -73,13 +74,13 @@ describe('makeUniflowComponent()', () => {
b: React.PropTypes.object
}
let Component = make(Child, {a: storeA, b: storeB})
React.render(<Component/>, domNode)
ReactDOM.render(<Component/>, domNode)
})

it('re-renders when any store prop emits change event', () => {
let Component = make(TestComponent, {a: storeA})
let render = sinon.spy(Component.prototype, 'render')
React.render(<Component/>, domNode)
ReactDOM.render(<Component/>, domNode)
sinon.assert.calledOnce(render)
storeA.emit('change')
sinon.assert.calledTwice(render)
Expand Down