一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - React - React.Children的用法詳解

React.Children的用法詳解

2022-02-23 16:15uuihoo React

這篇文章主要介紹了React.Children的用法詳解,幫助大家更好的理解和學習使用React框架,感興趣的朋友可以了解下

React.Children 是頂層API之一,為處理 this.props.children 這個封閉的數據結構提供了有用的工具。

this.props 對象的屬性與組件的屬性一一對應,但是有一個例外,就是 this.props.children 屬性。它表示組件的所有子節點。

1、React.Children.map

?
1
2
3
4
5
6
7
8
9
10
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>
})

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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 的值有三種可能:如果當前組件沒有子節點,它就是 undefined ;如果有一個子節點,數據類型是 object ;如果有多個子節點,數據類型就是 array 。所以,處理 this.props.children 的時候要小心。

React 提供一個工具方法 React.Children 來處理 this.props.children 。我們可以用 React.Children.map 來遍歷子節點,而不用擔心 this.props.children 的數據類型是 undefined 還是 object

傳入如下ReactElement:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<NotesList>
  <span>hello</span>
  <span>hello</span>
</NotesList>
//返回兩個子節點
 
 
<NotesList></NotesList>
//返回undefined
 
 
<NotesList>null</NotesList>
//返回null

2、React.Children.forEach

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

類似于 React.Children.map(),但是不返回對象。

3、React.Children.count

number React.Children.count(object children)

返回 children 當中的組件總數,和傳遞給 map 或者 forEach 的回調函數的調用次數一致。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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值:

?
1
2
3
4
5
6
7
8
9
10
11
<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 中 僅有的子級。否則拋出異常。

這里僅有的子級,only方法接受的參數只能是一個對象,不能是多個對象(數組)。

?
1
2
console.log(React.Children.only(this.props.children[0]));
//輸出對象this.props.children[0]

以上就是React.Children的用法詳解的詳細內容,更多關于React.Children的用法的資料請關注服務器之家其它相關文章!

原文鏈接:https://blog.csdn.net/uuihoo/article/details/79710318

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品亚洲永久免费精品 | 99精品视频在线观看免费 | 亚州在线播放 | 久久er国产精品免费观看2 | 好大好爽好涨太深了小喜 | 国产精品一区二区三区免费视频 | 日韩高清在线免费看 | 男人爱看的网站 | 国产精品一区牛牛影视 | 加勒比伊人 | 国产美女亚洲精品久久久久久 | 免费一级特黄特色大片在线观看 | 国产小嫩模好紧 | 男人的天堂日本 | 日韩亚洲人成在线 | 高清在线观看免费入口 | 精品国产日韩一区三区 | 国产成人在线播放视频 | 精精国产www视频在线观看免费 | 被黑人日 | 亚洲AV蜜桃永久无码精品无码网 | 99热久久这里只有精品6国产网 | 天天插在线视频 | japanesexxxx日本妞 | 色男人网 | 亚洲乱码尤物193yw在线播放 | 精品9e精品视频在线观看 | 91麻豆精品国产91久久久 | 天天色综合三 | yy111111影院理论大片 | 福利视频一区青娱 | 国产在线欧美精品 | 高级黄色片 | 6080窝窝理论| 精品日韩欧美一区二区三区 | freefron性中国 | 亚洲精品九色在线网站 | 亚洲国产精品婷婷久久久久 | 黑人巨 | 国产午夜亚洲精品 | 女同变态 中文字幕 |