React.Children的用法

React.Children是頂層API之一,為處理this.props.children這個(gè)封閉的數(shù)據(jù)結(jié)構(gòu)提供了有用的工具。

this.props對(duì)象的屬性與組件的屬性一一對(duì)應(yīng),但是有一個(gè)例外,就是this.props.children屬性。它表示組件的所有子節(jié)點(diǎn)。

1、React.Children.map

object React.Children.map(object children,function fn [, object context])

使用方法:

React.Children.map(this.props.children,function(child){

????return <li>{child}</li>

})

this.props.children.forEach(function(child){

???return <li>{child}</li>

})

在每一個(gè)直接子級(jí)(包含在 children 參數(shù)中的)上調(diào)用 fn 函數(shù),此函數(shù)中的 this 指向 上下文。如果 children 是一個(gè)內(nèi)嵌的對(duì)象或者數(shù)組,它將被遍歷:不會(huì)傳入容器對(duì)象到 fn 中。如果 children 參數(shù)是 null 或者 undefined,那么返回 null 或者 undefined 而不是一個(gè)空對(duì)象。

<script type="text/jsx">

var NotesList = React.createClass({

render:function() {

????return (

????????<ol>? {

????????????React.Children.map(this.props.children, function (child) {

????????????????return<li>{child}</li>;

? ? ? ? ? ? ? ? })

? ? ? ? ? }

</ol>);

? ? }

? });

React.render(

????<NotesList>

????????<span>hello</span>

????????<span>hello</span>

????</NotesList>,

????document.body );

</script>

這里需要注意,this.props.children的值有三種可能:如果當(dāng)前組件沒(méi)有子節(jié)點(diǎn),它就是undefined;如果有一個(gè)子節(jié)點(diǎn),數(shù)據(jù)類(lèi)型是object;如果有多個(gè)子節(jié)點(diǎn),數(shù)據(jù)類(lèi)型就是array。所以,處理this.props.children的時(shí)候要小心。

React提供一個(gè)工具方法React.Children來(lái)處理this.props.children。我們可以用React.Children.map來(lái)遍歷子節(jié)點(diǎn),而不用擔(dān)心this.props.children的數(shù)據(jù)類(lèi)型是undefined還是object。

傳入如下ReactElement:

<NotesList>

????<span>hello</span>

????<span>hello</span>

</NotesList> //返回兩個(gè)子節(jié)點(diǎn)

<NotesList></NotesList> //返回undefined

<NotesList>null</NotesList>//返回null

2、React.Children.forEach

React.Children.forEach(object children, function fn [, object context])

? 類(lèi)似于 React.Children.map(),但是不返回對(duì)象。?

3、React.Children.count

number React.Children.count(object children)

返回 children 當(dāng)中的組件總數(shù),和傳遞給 map 或者 forEach 的回調(diào)函數(shù)的調(diào)用次數(shù)一致。

render: function() {

? ? console.log(React.Children.count(this.props.children)); //2

? ? return (

? ? ? <ol>

? ? ? ? {

? ? ? ? ? this.props.children.forEach(function (child) {

? ? ? ? ? ? ? return <li>{child}</li>

? ? ? ? ? })

? ? ? ? }

? ? ? </ol>

? ? );

? }

不同的ReactElement,輸出count值:

<NotesList>

? ? <span>hello</span>

? ? <span>hello</span>

</NotesList>

console.log(React.Children.count(this.props.children)); //2

<NotesList></NotesList>

console.log(React.Children.count(this.props.children)); //0

<NotesList>null</NotesList>

console.log(React.Children.count(this.props.children)); //1

4、React.Children.only

object React.Children.only(object children)

返回children中僅有的子級(jí)。否則拋出異常。

這里僅有的子級(jí),only方法接受的參數(shù)只能是一個(gè)對(duì)象,不能是多個(gè)對(duì)象(數(shù)組)。

console.log(React.Children.only(this.props.children[0]));

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,142評(píng)論 0 2
  • 源站:http://fengyuanchen.github.io/viewer/ 應(yīng)用: html: 源碼上是正常...
    羊繪霖閱讀 5,691評(píng)論 0 2
  • 現(xiàn)在最熱門(mén)的前端框架,毫無(wú)疑問(wèn)是 React 。上周,基于 React 的 React Native 發(fā)布,結(jié)果一...
    sakura_L閱讀 489評(píng)論 0 0
  • 我是一只金絲雀, 我有這絢麗的羽毛, 漂亮的翅膀, 我的主人十分疼愛(ài)我。 可是 我卻永遠(yuǎn)被禁錮著 永遠(yuǎn)被禁錮在這牢...
    曉曉螢火蟲(chóng)閱讀 298評(píng)論 0 0
  • 我不談?wù)撎飿悻B具體的生活,也不談?wù)撎飿悻B和王石的關(guān)系,看完了《習(xí)慣就好》,我只想談?wù)勎以谶@本書(shū)里看到的內(nèi)容。在寫(xiě)這...
    Erin棋落閱讀 360評(píng)論 0 0

友情鏈接更多精彩內(nèi)容