快速入门 React指南 参考 Flux Tips

发布于 2015-07-12 09:55:50 | 555 次阅读 | 评论: 0 | 来源: 网络整理

通常,一个组件的子代(this.props.children)是一个组件的数组:

var GenericWrapper = React.createClass({
  componentDidMount: function() {
    console.log(Array.isArray(this.props.children)); // => true
  },

  render: function() {
    return <div />;
  }
});

React.render(
  <GenericWrapper><span/><span/><span/></GenericWrapper>,
  mountNode
);

然而,当只有一个子代的时候,this.props.children 将会变成一个单独的组件,而不是数组形式。这样就减少了数组的占用。

var GenericWrapper = React.createClass({
  componentDidMount: function() {
    console.log(Array.isArray(this.props.children)); // => false

    // 注意:结果将是 5,而不是 1,因为 `this.props.children` 不是数组,而是 'hello' 字符串!
    console.log(this.props.children.length);
  },

  render: function() {
    return <div />;
  }
});

React.render(<GenericWrapper>hello</GenericWrapper>, mountNode);

为了让处理 this.props.children 更简单,我们提供了 React.Children utilities

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务