From 710f54b60560d16bf88a97aafa9f5541621c8561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Tue, 12 Nov 2013 12:59:03 -0800 Subject: [PATCH] Children props tip tweak --- tips/07-children-props-type.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tips/07-children-props-type.md b/tips/07-children-props-type.md index 675b2f74..8c4549ac 100644 --- a/tips/07-children-props-type.md +++ b/tips/07-children-props-type.md @@ -7,7 +7,7 @@ prev: style-props-value-px.html next: controlled-input-null-value.html --- -Usually, a component's `this.props.children` is an array of components: +Usually, a component's children (`this.props.children`) is an array of components: ```js /** @jsx React.DOM */ @@ -22,21 +22,23 @@ var GenericWrapper = React.createClass({ }); React.renderComponent( - , + , mountNode ); ``` -To save an extra array allocation, it returns the component itself _without the array wrapper_ when there's only one child. +However, when there is only a single child, `this.props.children` will be the single child component itself _without the array wrapper_. This saves an array allocation. ```js /** @jsx React.DOM */ var GenericWrapper = React.createClass({ componentDidMount: function() { - // **warning**: yields 5 for length of the string 'hello', not 1 for the + console.log(Array.isArray(this.props.children)); // => false + + // warning: yields 5 for length of the string 'hello', not 1 for the // length of the non-existant array wrapper! - console.log(this.props.children.length); + console.log(this.props.children.length); }, render: function() { return
;