Avoid having react interface methods call each other

Factor out the common bits of componentWillMount and componentWillReceiveProps
to a common function.
pull/21833/head
Richard van der Hoff 2016-04-21 14:14:08 +01:00
parent e5e9a3819e
commit f09861794d
1 changed files with 10 additions and 4 deletions

View File

@ -21,16 +21,22 @@ module.exports = React.createClass({
},
componentWillMount: function() {
this.children = {};
this.nodes = {};
this.componentWillReceiveProps(this.props);
this._updateChildren(this.props.children);
},
componentWillReceiveProps: function(nextProps) {
this._updateChildren(nextProps.children);
},
/**
* update `this.children` according to the new list of children given
*/
_updateChildren: function(newChildren) {
var self = this;
var oldChildren = this.children;
var oldChildren = this.children || {};
this.children = {};
React.Children.toArray(nextProps.children).forEach(function(c) {
React.Children.toArray(newChildren).forEach(function(c) {
if (oldChildren[c.key]) {
var old = oldChildren[c.key];
var oldNode = ReactDom.findDOMNode(self.nodes[old.key]);