From f09861794d41c62238a9dbbeea4ffcc171114d18 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 21 Apr 2016 14:14:08 +0100 Subject: [PATCH] Avoid having react interface methods call each other Factor out the common bits of componentWillMount and componentWillReceiveProps to a common function. --- src/Velociraptor.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Velociraptor.js b/src/Velociraptor.js index a3e013a2b4..ad12d1323b 100644 --- a/src/Velociraptor.js +++ b/src/Velociraptor.js @@ -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]);