Skip to content

Commit f64d1b0

Browse files
committed
Trigger state change using componentDidMount/Update instead of setState callback
1 parent 90b2d66 commit f64d1b0

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

modules/components/Routes.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,16 @@ function runHooks(hooks, callback) {
151151
}
152152

153153
function updateMatchComponents(matches, refs) {
154-
matches.forEach(function (match) {
154+
var match;
155+
for (var i = 0, len = matches.length; i < len; ++i) {
156+
match = matches[i];
155157
match.component = refs.__activeRoute__;
158+
159+
if (match.component == null)
160+
break; // End of the tree.
161+
156162
refs = match.component.refs;
157-
});
163+
}
158164
}
159165

160166
function returnNull() {
@@ -195,9 +201,16 @@ var Routes = React.createClass({
195201
},
196202

197203
componentDidMount: function () {
198-
if (this._initialSetStateCallback) {
199-
this._initialSetStateCallback();
200-
delete this._initialSetStateCallback;
204+
if (this._handleStateChange) {
205+
this._handleStateChange();
206+
delete this._handleStateChange;
207+
}
208+
},
209+
210+
componentDidUpdate: function () {
211+
if (this._handleStateChange) {
212+
this._handleStateChange();
213+
delete this._handleStateChange;
201214
}
202215
},
203216

@@ -243,26 +256,19 @@ var Routes = React.createClass({
243256
} else if (abortReason) {
244257
this.goBack();
245258
} else {
246-
var handleStateChange = function () {
247-
updateMatchComponents(this.state.matches, this.refs);
259+
this._handleStateChange = this.handleStateChange.bind(this, path, actionType);
260+
this.setState(nextState);
261+
}
262+
}.bind(this));
263+
},
248264

249-
this.updateScroll(path, actionType);
265+
handleStateChange: function (path, actionType) {
266+
updateMatchComponents(this.state.matches, this.refs);
250267

251-
if (this.props.onChange)
252-
this.props.onChange.call(this);
253-
}.bind(this);
268+
this.updateScroll(path, actionType);
254269

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-
// https://github.com/facebook/react/blob/3bbed150ab58a07b0c4faf64126b4c9349eecfea/src/core/ReactCompositeComponent.js#L900
261-
this._initialSetStateCallback = handleStateChange;
262-
this.setState(nextState);
263-
}
264-
}
265-
}.bind(this));
270+
if (this.props.onChange)
271+
this.props.onChange.call(this);
266272
},
267273

268274
/**

0 commit comments

Comments
 (0)