一文詳解如何React中實(shí)現(xiàn)插槽
React中實(shí)現(xiàn)插槽
設(shè)計(jì)插槽
在React中實(shí)現(xiàn)插槽需要我們自己來(lái)實(shí)現(xiàn) 主要用到props.children
我們以跟組件作為父組件
創(chuàng)建子組件DemoOne組件
import React from "react"; import ReactDOM from "react-dom/client"; import DemoOne from "./views/DemoOne"; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <> <DemoOne title="我是標(biāo)題" x={10}> <span>哈哈哈</span> <span>呵呵呵</span> </DemoOne> <DemoOne title="嘿嘿嘿"> <span>嘿嘿嘿</span> </DemoOne> <DemoOne title="哈哈哈" /> </> );
import React from "react"; const DemoOne = function DemoOne(props) { let {title, x, children } = props; console.log(children); return ( <div className="demo-BOX"> {children} </div> ); }; DemoOne.propTypes = { title: PropTypes.string.isRequired, x: PropTypes.number, y: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]), }; export default DemoOne;
這里我們引入了三次子組件
我們先看看子組件中返回的children是什么
如果我們要控制每個(gè)位置渲染不一樣的插槽內(nèi)容
方式一 是使用數(shù)組的形式 但是無(wú)法保證每次傳入的都是多個(gè)插槽值
這時(shí)需要使用React.Children 對(duì)象中提供的額外方法 對(duì)props.children做處理: 其上有count\forEach\map\toArray等方法
在這些方法內(nèi)部 已經(jīng)對(duì)children做了各種形式的處理
我們可以直接使用
import React from "react"; const DemoOne = function DemoOne(props) { let { title, x, children } = props; if (!children) { children = []; } else if (!Array.isArray(children)) { children = [children]; } console.log(children); return ( <div className="demo-BOX"> {children[0]} {children[1]} </div> ); }; export default DemoOne;
具名插槽
當(dāng)我們?cè)诟附M件中對(duì)要插入的內(nèi)容設(shè)置上名字后 想要依據(jù)不同的名字 渲染在不同的位置 并且順序也不同時(shí) 我們可以采用具名插槽的方式
這里我們?cè)O(shè)置了footer與header
import React from "react"; import ReactDOM from "react-dom/client"; import DemoOne from "./views/DemoOne"; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <> <DemoOne title="我是標(biāo)題" x={10}> <span slot='footer' >哈哈哈</span> <span slot='header' >呵呵呵</span> </DemoOne> <DemoOne title="嘿嘿嘿"> <span>嘿嘿嘿</span> </DemoOne> <DemoOne title="哈哈哈" /> </> );
我們可以先使用React.Children.toArray() 將children都變?yōu)閿?shù)組形式
因?yàn)閭鬟f進(jìn)來(lái)的插槽信息 都是編譯為virtualDOM后傳遞進(jìn)來(lái)的 而不是傳遞的標(biāo)簽
所以我們可以直接通過(guò).語(yǔ)法來(lái)獲取到props對(duì)象的slot屬性
這里定義三個(gè)數(shù)組用來(lái)存放 header footer 與 default
import React from "react"; const DemoOne = function DemoOne(props) { let { title, x, children } = props; children = React.Children.toArray(children); let headerSlot = [], footerSlot = [], defaultSlot = []; children.forEach((child) => { //傳遞進(jìn)來(lái)的插槽信息 都是編譯為virtualDOM后傳遞進(jìn)來(lái)的 而不是傳遞的標(biāo)簽 let { slot } = child.props; if (slot === "header") { headerSlot.push(child); } else if (slot === "footer") { footerSlot.push(child); } else { defaultSlot.push(child); } }); return ( <div className="demo-BOX"> {headerSlot} <br /> <h2 className="title">{title}</h2> <span>{x}</span> <br /> {footerSlot} </div> ); }; export default DemoOne;
到此這篇關(guān)于一文詳解如何React中實(shí)現(xiàn)插槽的文章就介紹到這了,更多相關(guān)React插槽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react antd表格中渲染一張或多張圖片的實(shí)例
這篇文章主要介紹了react antd表格中渲染一張或多張圖片的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10React之防止按鈕多次點(diǎn)擊事件?重復(fù)提交
這篇文章主要介紹了React之防止按鈕多次點(diǎn)擊事件?重復(fù)提交問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10redux功能強(qiáng)大的Middleware中間件使用學(xué)習(xí)
這篇文章主要為大家介紹了redux功能強(qiáng)大的Middleware中間件使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09React不能將useMemo設(shè)置為默認(rèn)方法原因詳解
這篇文章主要為大家介紹了React不能將useMemo設(shè)置為默認(rèn)方法原因詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-07-07在create-react-app中使用css modules的示例代碼
這篇文章主要介紹了在create-react-app中使用css modules的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07詳解React如何實(shí)現(xiàn)代碼分割Code Splitting
這篇文章主要為大家介紹了React如何實(shí)現(xiàn)代碼分割Code Splitting示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08React錯(cuò)誤邊界Error Boundaries
錯(cuò)誤邊界是一種React組件,這種組件可以捕獲發(fā)生在其子組件樹(shù)任何位置的JavaScript錯(cuò)誤,并打印這些錯(cuò)誤,同時(shí)展示降級(jí)UI,而并不會(huì)渲染那些發(fā)生崩潰的子組件樹(shù)2023-01-01React實(shí)現(xiàn)動(dòng)效彈窗組件
最近在使用react開(kāi)發(fā)項(xiàng)目,遇到這樣一個(gè)需求實(shí)現(xiàn)一個(gè)帶有動(dòng)效的 React 彈窗組件,如果不考慮動(dòng)效,很容易實(shí)現(xiàn),接下來(lái)小編通過(guò)本文給大家介紹React實(shí)現(xiàn)動(dòng)效彈窗組件的實(shí)現(xiàn)代碼,一起看看吧2021-06-06