Skip to content

Commit b510c0d

Browse files
committed
Merge pull request #384 from gaearon/initial-fixes
Fix redirect and fire onChange when rendering for the first time
2 parents d47d7dd + ea5a380 commit b510c0d

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
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
},

modules/mixins/LocationContext.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,13 @@ var LocationContext = {
4747

4848
if (location) {
4949
PathStore.setup(location);
50+
PathStore.addChangeListener(this.handlePathChange);
5051

5152
if (this.updateLocation)
5253
this.updateLocation(PathStore.getCurrentPath(), PathStore.getCurrentActionType());
5354
}
5455
},
5556

56-
componentDidMount: function () {
57-
if (this.getLocation())
58-
PathStore.addChangeListener(this.handlePathChange);
59-
},
60-
6157
componentWillUnmount: function () {
6258
if (this.getLocation())
6359
PathStore.removeChangeListener(this.handlePathChange);

0 commit comments

Comments
 (0)