当前位置: 首页 > news >正文

【React】插槽渲染机制

目录

  • 通过 children 属性结合条件渲染
  • 通过 children 和 slot 属性实现具名插槽
  • 通过 props 实现具名插槽

在 React 中,并没有直接类似于 Vue 中的“插槽”机制(slot)。但是,React 可以通过 propschildren 来实现类似插槽的功能,允许你将组件的内容进行灵活插入和替换。

通过 children 属性结合条件渲染

通过 children 来传递任意数量的子元素,然后在组件内部通过位置进行条件渲染,从而实现插槽功能。

Layout 组件通过 children 渲染传递给它的所有子元素,而这些子元素可以是任何内容,类似于 Vue 中的默认插槽。虽然在某些情况下,children 已经是一个数组(例如多个子元素的情况),但 React.Children.toArray 会确保你始终获得一个标准化的数组(过滤掉null、undefined等数据)。

//子组件
const Layout = ({ children }) => {children = React.Children.toArray(children);console.log(children,'children')return (<div className="layout"><header>Header</header><main>{children}</main> {/* 这里的 children 就是父组件传递进来的内容 */}<footer>Footer</footer></div>);
}; 
//父组件
const App = () => {return (<Layout><h1>Hello, React!</h1><p>This is the main content of the page.</p></Layout>);
};

打印出children,就是父组件标签里内容编译成的virtualDOM。

在这里插入图片描述

通过 children 和 slot 属性实现具名插槽

在标签上加slot来标记标签

//父组件
root.render(<><DemoOne title="REACT好好玩哦" x={10}><span slot="footer">我是页脚</span><span>哈哈哈哈</span><span slot="header">我是页眉</span></DemoOne></>
); 

子组件根据children属性中的slot来区分插槽

//子组件
const DemoOne = function DemoOne(props) {let { title, x, children } = props;children = React.Children.toArray(children);let headerSlot = [],footerSlot = [],defaultSlot = [];children.forEach(child => {// 传递进来的插槽信息,都是编译为virtualDOM后传递进来的「而不是传递的标签」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>;
};

通过 props 实现具名插槽

显式传递 props 来模拟具名插槽,传递不同的内容到特定的插槽位置

const DemoOne = ({ title, x, children, footer, header }) => {return (<div className="demo-box"><h1>{title}</h1><div>{header}</div> {/* 渲染具名插槽 header */}<div>{children}</div> {/* 渲染默认插槽 */}<div>{footer}</div> {/* 渲染具名插槽 footer */}</div>);
};const App = () => {return (<DemoOne title="REACT好好玩哦" x={10} footer={<span>我是页脚</span>} header={<span>我是页眉</span>}><span>哈哈哈哈</span></DemoOne>);
};

在这里插入图片描述

http://www.lryc.cn/news/522876.html

相关文章:

  • 计算机网络 | 什么是公网、私网、NAT?
  • 如何解决Outlook无法连接到服务器的问题
  • vue2 web 多标签输入框 elinput是否当前焦点
  • 32单片机综合应用案例——物联网(IoT)环境监测站(四)(内附详细代码讲解!!!)
  • LabVIEW与WPS文件格式的兼容性
  • 小结: 路由协议的演进和分类
  • OpenCV相机标定与3D重建(60)用于立体校正的函数stereoRectify()的使用
  • Android wifi列表中去自身的热点
  • Windows环境本地配置pyspark环境详细教程
  • 《自动驾驶与机器人中的SLAM技术》ch9:自动驾驶车辆的离线地图构建
  • IP属地会随着人的移动而改变吗
  • openharmony应用开发快速入门
  • USB3020任意波形发生器4路16位同步模拟量输出卡1MS/s频率 阿尔泰科技
  • 云消息队列 Kafka 版 V3 系列荣获信通院“云原生技术创新标杆案例”
  • linux下的NFS和FTP部署
  • JS Clipboard API
  • MySQL中大量数据优化方案
  • 重拾Python学习,先从把python删除开始。。。
  • centos 安全配置基线
  • 高级编程语言的基本语法在CPU的眼中是什么样的呢?
  • Redis 性能优化:多维度技术解析与实战策略
  • .netframwork模拟启动webapi服务并编写对应api接口
  • MongoDB 学习指南与资料分享
  • 【Azure 架构师学习笔记】- Azure Function (2) --实操1
  • 扫描深度?滤光片和偏振片区别?
  • HJ4 字符串分隔(Java版)
  • 【脑机接口数据处理】matlab读取ns6 NS6 ns5NS5格式脑电数据
  • 用C++实现一个基于模板的观察者设计模式
  • 【华为路由/交换机的ftp文件操作】
  • 微信小程序 实现拼图功能