鍍金池/ 教程/ HTML/ 子 props 的類型
顯示數(shù)據(jù)
組件的引用
Controlled Input 值為 null 的情況
Reconciliation
子 props 的類型
組件的詳細(xì)說(shuō)明和生命周期
傳遞 Props
特殊的非 DOM 屬性
組件 API
PureRenderMixin
雙向綁定輔助工具
瀏覽器中的工作原理
深入 JSX
表單組件
Dangerously Set innerHTML
入門(mén)
JSX 中的 If-Else
克隆組件
教程
更多的關(guān)于Refs
JSX 的 false 處理
高級(jí)性能
Mounting 后 componentWillReceiveProps 未被觸發(fā)
簡(jiǎn)介
測(cè)試工具集
JSX 陷阱
工具集成(ToolingIntegration)
公開(kāi)組件功能
通過(guò) AJAX 加載初始數(shù)據(jù)
事件系統(tǒng)
可復(fù)用組件
this.props.children undefined
不可變數(shù)據(jù)的輔助工具(Immutability Helpers)
動(dòng)態(tài)交互式用戶界面
組件的 DOM 事件監(jiān)聽(tīng)
復(fù)合組件
動(dòng)畫(huà)
插件
JSX 展開(kāi)屬性
行內(nèi)樣式
性能分析工具
類名操作
與其他類庫(kù)并行使用 React
鍵控的片段
標(biāo)簽和屬性支持
組件間的通信
React (虛擬)DOM 術(shù)語(yǔ)
JSX 根節(jié)點(diǎn)的最大數(shù)量
在樣式props中快速制定像素值
頂層 API
深入理解 React
自閉合標(biāo)簽
為什么使用 React?
getInitialState 里的 Props 是一個(gè)反模式
與 DOM 的差異

子 props 的類型

通常,一個(gè)組件的子代(this.props.children)是一個(gè)組件的數(shù)組:

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
);

然而,當(dāng)只有一個(gè)子代的時(shí)候,this.props.children 將會(huì)變成一個(gè)單獨(dú)的組件,而不是數(shù)組形式。這樣就減少了數(shù)組的占用。

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

    // 注意:結(jié)果將是 5,而不是 1,因?yàn)?`this.props.children` 不是數(shù)組,而是 'hello' 字符串!
    console.log(this.props.children.length);
  },

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

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

為了讓處理 this.props.children 更簡(jiǎn)單,我們提供了 React.Children utilities。