首頁 > web前端 > js教程 > 主體

React 中 props.children 的完整指南

WBOY
發布: 2024-07-17 08:35:50
原創
721 人瀏覽過

A Complete Guide To props.children In React

我使用 React 開發了幾個月,但沒有完全理解 React 元件模型的真正威力。有一天,我決定深入研究作曲,這就是我學到的東西。

React 元件和子元件

在 React 中,一個元件可以有一個、多個子元件,也可以沒有子元件。很好,但是等等 —— 什麼是「孩子」?我們用一個例子來解釋:

雷雷

Profile 元件有兩個子元件:ProfileImage 和 ProfileDetails,而這兩個元件沒有子元件.

「在同時包含開始標籤和結束標籤的 JSX 表達式中,這些標籤之間的內容會以特殊 props 傳遞:props.children」 — React 文件

本質上,props.children是一個特殊的prop,自動傳遞給每個元件,可用於在呼叫元件時渲染開始和結束標籤之間包含的內容。這些類型的元件被官方文件標識為“boxes”。

透過 JSX 語法識別組件

在 React 的 JSX 中,帶有子組件的元件始終由開始標籤和結束標籤標識。每個子項都必須放置在這兩個標籤之間,就像我們在上面看到的那樣。當元件沒有子元件時,您可以使用 來呼叫它。或 ,但通常首選後一種語法。自閉合標籤的目的是使程式碼更短、更易於閱讀。

操作中的 props.children

假設我們要建立一個 ImageSlider 元件。我們的目標是像這樣呼叫元件:

雷雷

可以看到,ImageSlider是由幾個React 中 props.children 的完整指南組成的可以透過 props.children 存取和渲染。

雷雷

感謝 props.children 我們可以將內容嵌套在組件中,就像嵌套常見的 HTML 元素一樣.

props.children 允許使用哪些類型的內容?

透過 props.children 傳遞給組件的內容可以包含

undefined、null、布林值、數字、字串、React 元素或任何這些類型的遞歸數組。它也可以是傳回這些類型之一的函數。

請注意,如 React 文件中所提到的, false、null、undefined 和 true 是有效的子元素,但它們將被忽略並且不會渲染。如果想要渲染 false、true、null 或 undefined,則必須先將其轉換為字串:

雷雷

為什麼 props.children 如此重要?

props.children 允許我們組合組件,從而組合我們的前端介面;利用 React 組件模型的真正力量。

「React 擁有強大的組合模型,我們建議使用組合而不是繼承來在元件之間重用程式碼。」 — React 文件

正如官方文件中所述,有時您可能需要填充組件中的多個「漏洞」。在這種情況下,定義多個自訂 props 可能是更好的方法,而不是使用 props.children;如下例所示:

雷雷

React.Children

「React.Children 提供了處理 props.children 不透明資料結構的實用程式」 — React 文件

為什麼 props.children 是一個「不透明的資料結構」?

因為 props.children 可以由一個、多個或沒有子元素組成,這意味著它的值可以是單個子節點,一組子節點,或者未定義分別。感謝 React.Children API,我們可以輕鬆處理 props.children,而無需考慮其每種可能的類型。值得慶幸的是,一切都會在後台為我們處理。

在撰寫本文時,React.Children 提供了五種不同的實用程式

  • 地圖

  • 對於每個

  • 數數

  • 到陣列

讓我們透過一個例子來看看如何使用 React.Children。假設我們想要將特殊的 CSS 類別 img-special-class 加入到 ImageSlider 元件的每個子元件中。這可以如下完成:

export default function ImageSlider(props) {
  const { children } = props

  return (
    
{ React.Children.map(children, (child) => React.cloneElement(child, { className: `${child.props.className} img-special-class` }) ) }
); }
登入後複製

React.Children.map allows us to iterate over props.children and transform each element according to the function passed as the second parameter. To achieve our goal, we used React.cloneElement. This is because we needed to change the value of the className prop, but props are immutable in React, so we had to clone each child.

Conclusion

Mastering props.children is essential to becoming a great React developer and beginning to harness the full potential of the React component model. props.children is one of React's most useful features, since it gives us the ability to render child components. Because of this, every developer should know how to use it properly.

I hope this article helps you master composition in React.


The post "A Complete Guide To props.children In React" appeared first on Writech.

以上是React 中 props.children 的完整指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!