@@ -130,12 +130,17 @@ var Index = React.createClass({
130130} ) ;
131131
132132var Contact = React . createClass ( {
133- getInitialState : function ( ) {
133+ getStateFromStore : function ( props ) {
134+ props = props || this . props ;
134135 return {
135- contact : ContactStore . getContact ( this . props . params . id )
136+ contact : ContactStore . getContact ( props . params . id )
136137 } ;
137138 } ,
138139
140+ getInitialState : function ( ) {
141+ return this . getStateFromStore ( ) ;
142+ } ,
143+
139144 componentDidMount : function ( ) {
140145 ContactStore . addChangeListener ( this . updateContact ) ;
141146 } ,
@@ -144,13 +149,15 @@ var Contact = React.createClass({
144149 ContactStore . removeChangeListener ( this . updateContact ) ;
145150 } ,
146151
152+ componentWillReceiveProps : function ( newProps ) {
153+ this . setState ( this . getStateFromStore ( newProps ) ) ;
154+ } ,
155+
147156 updateContact : function ( ) {
148157 if ( ! this . isMounted ( ) )
149158 return ;
150159
151- this . setState ( {
152- contact : ContactStore . getContact ( this . props . params . id )
153- } ) ;
160+ this . setState ( this . getStateFromStore ( ) )
154161 } ,
155162
156163 destroy : function ( ) {
@@ -204,20 +211,6 @@ var NotFound = React.createClass({
204211 }
205212} ) ;
206213
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-
221214// Request utils.
222215
223216function getJSON ( url , cb ) {
@@ -249,3 +242,18 @@ function deleteJSON(url, cb) {
249242 req . open ( 'DELETE' , url ) ;
250243 req . send ( ) ;
251244}
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