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

宁波建站平台网站推广与优化平台

宁波建站平台,网站推广与优化平台,网站优化改版,网页制作技术有哪些目录 通过 children 属性结合条件渲染通过 children 和 slot 属性实现具名插槽通过 props 实现具名插槽 在 React 中,并没有直接类似于 Vue 中的“插槽”机制(slot)。但是,React 可以通过 props和 children 来实现类似插槽的功能…

目录

  • 通过 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.hkea.cn/news/298786/

相关文章:

  • 做网站空间哪家好windows7系统优化工具
  • 网站建设首选公司seo推广一个月见效
  • 微信做模板下载网站有哪些推广网站要注意什么
  • 做网站 java c常德seo快速排名
  • 仙桃做网站找谁常用的网络推广方法
  • 品牌推广网站怎样做百度手机助手苹果版
  • 武汉工业网站制作百度人工服务热线24小时
  • 新闻头条最新消息今日头条站长之家seo综合
  • app与网站宁波seo网络推广渠道介绍
  • 国外学做咖啡的网站百度高级搜索网址
  • 建网站开源代码游戏推广怎么找玩家
  • 莱州哪里有做网站的浙江网站建设平台
  • ps网站设计与制作免费推广seo
  • 网站查询功能怎么做关键词搜索量怎么查
  • 付费网站推广网站优化包括哪些内容
  • 在日本做色情网站广州seo外包
  • 最棒的网站建设考研最靠谱的培训机构
  • 广州建设企业网站黑河seo
  • 招商网站建设性价比高seo排名优化的
  • 产品网站怎么做的长沙正规关键词优化价格从优
  • 怎样查询江西省城乡建设厅网站杭州seo网
  • 网站建设空间是指什么软件网站优化最为重要的内容是
  • 做美工要开通什么网站的会员呢新网站友链
  • 网站集约化建设推进情况推广app赚钱
  • 番禺大石做网站域名污染查询网站
  • 长沙市在建工程项目免费seo快速排名工具
  • 南宁定制网站制作电话图片外链生成工具
  • 哪些网站做的海报比较高大上百度客服电话是多少
  • 菏泽网站建设电话常州seo外包
  • 做木皮的网站裂变营销五种模式十六种方法