Skip to content

Commit ea5a380

Browse files
committed
[fixed] Make sure onChange is fired at synchronous first render
1 parent dee374f commit ea5a380

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

modules/components/Routes.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ var Routes = React.createClass({
194194
};
195195
},
196196

197+
componentDidMount: function () {
198+
if (this._initialSetStateCallback) {
199+
this._initialSetStateCallback();
200+
delete this._initialSetStateCallback;
201+
}
202+
},
203+
197204
/**
198205
* Performs a depth-first search for the first route in the tree that matches on
199206
* the given path. Returns an array of all routes in the tree leading to the one
@@ -236,14 +243,23 @@ var Routes = React.createClass({
236243
} else if (abortReason) {
237244
this.goBack();
238245
} else {
239-
this.setState(nextState, function () {
246+
var handleStateChange = function () {
240247
updateMatchComponents(this.state.matches, this.refs);
241248

242249
this.updateScroll(path, actionType);
243250

244251
if (this.props.onChange)
245252
this.props.onChange.call(this);
246-
}.bind(this));
253+
}.bind(this);
254+
255+
if (this.isMounted()) {
256+
this.setState(nextState, handleStateChange);
257+
} else {
258+
// React does not invoke setState callback if we're still mounting
259+
// so we have to store it and invoke in componentDidMount.
260+
this._initialSetStateCallback = handleStateChange;
261+
this.setState(nextState);
262+
}
247263
}
248264
}.bind(this));
249265
},

0 commit comments

Comments
 (0)