Skip to content

Commit 07debf2

Browse files
committed
AltContainer changes on props change
1 parent 3f014e2 commit 07debf2

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

components/AltContainer.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,30 @@ var AltContainer = React.createClass({
5353
throw new ReferenceError('Cannot define both store and stores')
5454
}
5555

56-
return this.getStateFromStores() || {}
56+
return this.getStateFromStores(this.props) || {}
57+
},
58+
59+
componentWillReceiveProps: function (nextProps) {
60+
this.destroySubscriptions()
61+
this.setState(this.getStateFromStores(nextProps))
62+
this.registerStores(nextProps)
5763
},
5864

5965
componentDidMount: function () {
66+
this.registerStores(this.props)
67+
},
68+
69+
componentWillUnmount: function () {
70+
this.destroySubscriptions()
71+
},
72+
73+
registerStores: function (props) {
6074
Subscribe.create(this)
6175

62-
if (this.props.store) {
63-
this.addSubscription(this.props.store)
64-
} else if (this.props.stores) {
65-
var stores = this.props.stores
76+
if (props.store) {
77+
this.addSubscription(props.store)
78+
} else if (props.stores) {
79+
var stores = props.stores
6680

6781
if (Array.isArray(stores)) {
6882
stores.forEach(function (store) {
@@ -76,26 +90,26 @@ var AltContainer = React.createClass({
7690
}
7791
},
7892

79-
componentWillUnmount: function () {
93+
destroySubscriptions: function () {
8094
Subscribe.destroy(this)
8195
},
8296

83-
getStateFromStores: function () {
84-
if (this.props.store) {
85-
return getState(this.props.store, this.props)
86-
} else if (this.props.stores) {
87-
var stores = this.props.stores
97+
getStateFromStores: function (props) {
98+
if (props.store) {
99+
return getState(props.store, props)
100+
} else if (props.stores) {
101+
var stores = props.stores
88102

89103
// If you pass in an array of stores the state is merged together.
90104
if (Array.isArray(stores)) {
91105
return stores.reduce(function (obj, store) {
92-
return assign(obj, getState(store, this.props))
106+
return assign(obj, getState(store, props))
93107
}.bind(this), {})
94108

95109
// if it is an object then the state is added to the key specified
96110
} else {
97111
return Object.keys(stores).reduce(function (obj, key) {
98-
obj[key] = getState(stores[key], this.props)
112+
obj[key] = getState(stores[key], props)
99113
return obj
100114
}.bind(this), {})
101115
}
@@ -104,14 +118,14 @@ var AltContainer = React.createClass({
104118
}
105119
},
106120

107-
addSubscription: function(store) {
121+
addSubscription: function (store) {
108122
if (typeof store === 'object') {
109123
Subscribe.add(this, store, this.altSetState)
110124
}
111125
},
112126

113127
altSetState: function () {
114-
this.setState(this.getStateFromStores())
128+
this.setState(this.getStateFromStores(this.props))
115129
},
116130

117131
getProps: function () {

test/store-listener-component-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,5 +372,35 @@ export default {
372372
)
373373
})
374374
},
375+
376+
'changing an already mounted components props'() {
377+
let cb = null
378+
379+
const El = React.createClass({
380+
getInitialState() {
381+
return { store: TestStore }
382+
},
383+
384+
componentDidMount() {
385+
cb = state => this.setState(state)
386+
},
387+
388+
render() {
389+
return (
390+
<AltContainer ref="test" store={this.state.store}>
391+
<span />
392+
</AltContainer>
393+
)
394+
}
395+
})
396+
397+
const node = TestUtils.renderIntoDocument(<El />)
398+
399+
assert(node.refs.test.props.store === TestStore, 'node gets first state')
400+
401+
cb({ store: Store2 })
402+
403+
assert(node.refs.test.props.store === Store2, 'node changes props properly')
404+
},
375405
}
376406
}

0 commit comments

Comments
 (0)