@@ -130,12 +130,17 @@ var Index = React.createClass({
130
130
} ) ;
131
131
132
132
var Contact = React . createClass ( {
133
- getInitialState : function ( ) {
133
+ getStateFromStore : function ( props ) {
134
+ props = props || this . props ;
134
135
return {
135
- contact : ContactStore . getContact ( this . props . params . id )
136
+ contact : ContactStore . getContact ( props . params . id )
136
137
} ;
137
138
} ,
138
139
140
+ getInitialState : function ( ) {
141
+ return this . getStateFromStore ( ) ;
142
+ } ,
143
+
139
144
componentDidMount : function ( ) {
140
145
ContactStore . addChangeListener ( this . updateContact ) ;
141
146
} ,
@@ -144,13 +149,15 @@ var Contact = React.createClass({
144
149
ContactStore . removeChangeListener ( this . updateContact ) ;
145
150
} ,
146
151
152
+ componentWillReceiveProps : function ( newProps ) {
153
+ this . setState ( this . getStateFromStore ( newProps ) ) ;
154
+ } ,
155
+
147
156
updateContact : function ( ) {
148
157
if ( ! this . isMounted ( ) )
149
158
return ;
150
159
151
- this . setState ( {
152
- contact : ContactStore . getContact ( this . props . params . id )
153
- } ) ;
160
+ this . setState ( this . getStateFromStore ( ) )
154
161
} ,
155
162
156
163
destroy : function ( ) {
@@ -204,20 +211,6 @@ var NotFound = React.createClass({
204
211
}
205
212
} ) ;
206
213
207
- var routes = (
208
- < Route handler = { App } >
209
- < DefaultRoute handler = { Index } />
210
- < Route name = "new" path = "contact/new" handler = { NewContact } />
211
- < Route name = "not-found" path = "contact/not-found" handler = { NotFound } />
212
- < Route name = "contact" path = "contact/:id" handler = { Contact } />
213
- </ Route >
214
- ) ;
215
-
216
- React . renderComponent (
217
- < Routes children = { routes } /> ,
218
- document . getElementById ( 'example' )
219
- ) ;
220
-
221
214
// Request utils.
222
215
223
216
function getJSON ( url , cb ) {
@@ -249,3 +242,18 @@ function deleteJSON(url, cb) {
249
242
req . open ( 'DELETE' , url ) ;
250
243
req . send ( ) ;
251
244
}
245
+
246
+ var routes = (
247
+ < Route handler = { App } >
248
+ < DefaultRoute handler = { Index } />
249
+ < Route name = "new" path = "contact/new" handler = { NewContact } />
250
+ < Route name = "not-found" path = "contact/not-found" handler = { NotFound } />
251
+ < Route name = "contact" path = "contact/:id" handler = { Contact } />
252
+ </ Route >
253
+ ) ;
254
+
255
+ React . renderComponent (
256
+ < Routes children = { routes } /> ,
257
+ document . getElementById ( 'example' )
258
+ ) ;
259
+
0 commit comments