Skip to content

Commit

Permalink
Trigger state change using componentDidMount/Update instead of setSta…
Browse files Browse the repository at this point in the history
…te callback
  • Loading branch information
mjackson committed Oct 10, 2014
1 parent 90b2d66 commit f64d1b0
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions modules/components/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,16 @@ function runHooks(hooks, callback) {
}

function updateMatchComponents(matches, refs) {
matches.forEach(function (match) {
var match;
for (var i = 0, len = matches.length; i < len; ++i) {
match = matches[i];
match.component = refs.__activeRoute__;

if (match.component == null)
break; // End of the tree.

refs = match.component.refs;
});
}
}

function returnNull() {
Expand Down Expand Up @@ -195,9 +201,16 @@ var Routes = React.createClass({
},

componentDidMount: function () {
if (this._initialSetStateCallback) {
this._initialSetStateCallback();
delete this._initialSetStateCallback;
if (this._handleStateChange) {
this._handleStateChange();
delete this._handleStateChange;
}
},

componentDidUpdate: function () {
if (this._handleStateChange) {
this._handleStateChange();
delete this._handleStateChange;
}
},

Expand Down Expand Up @@ -243,26 +256,19 @@ var Routes = React.createClass({
} else if (abortReason) {
this.goBack();
} else {
var handleStateChange = function () {
updateMatchComponents(this.state.matches, this.refs);
this._handleStateChange = this.handleStateChange.bind(this, path, actionType);
this.setState(nextState);
}
}.bind(this));
},

this.updateScroll(path, actionType);
handleStateChange: function (path, actionType) {
updateMatchComponents(this.state.matches, this.refs);

if (this.props.onChange)
this.props.onChange.call(this);
}.bind(this);
this.updateScroll(path, actionType);

if (this.isMounted()) {
this.setState(nextState, handleStateChange);
} else {
// React does not invoke setState callback if we're still mounting
// so we have to store it and invoke in componentDidMount.
// https://github.com/facebook/react/blob/3bbed150ab58a07b0c4faf64126b4c9349eecfea/src/core/ReactCompositeComponent.js#L900
this._initialSetStateCallback = handleStateChange;
this.setState(nextState);
}
}
}.bind(this));
if (this.props.onChange)
this.props.onChange.call(this);
},

/**
Expand Down

0 comments on commit f64d1b0

Please sign in to comment.