程序员鸡皮

文章 分类 评论
90 3 11

站点介绍

一名PHP全栈程序员的日常......

React中组件的插槽实现

abzzp 2025-03-24 27 0条评论 前端 React

首页 / 正文
本站是作为记录一名北漂程序员编程学习以及日常的博客,欢迎添加微信BmzhbjzhB咨询交流......

发布于2024-07-04

插槽是React中比较重要的一个概念,它允许你将组件的一部分内容替换为自定义内容。在React中,插槽的实现主要通过children属性来完成。下面我们来详细了解一下React中插槽的实现。

React中的插槽

我们先看下面例子:

import React,{Component} from "react";
import NavBar from "./nav-bar";
import NavBarTwo from "./nav-bar-two";

export class App extends Component{
    render(){
        const btn = <button>按钮2</button>
        
        return (
            <div>
                {/* 1.使用children实现插槽 */}
                <NavBar>
                    <button>按钮</button>
                    <h2>哈哈哈</h2>
                    <i>斜体文本</i>
                </NavBar>
                {/* 2.使用props实现插槽 */}
                <NavBarTwo leftSlot={btn} centerSlot={<h2>呵呵呵</h2>} rightSlot={<i>斜体2</i>}></NavBarTwo>
            </div>
        )
    }
}


export default App;

上面代码中,我们使用了两个组件,NavBarNavBarTwo,它们都是通过插槽来显示内容。NavBar组件通过children属性来接收内容,然后通过{this.props.children}来显示内容。NavBarTwo组件通过props来接收内容,然后通过{this.props.leftSlot}{this.props.centerSlot}{this.props.rightSlot}来显示内容。

NavBar组件

我们先来看一下NavBar组件的实现:

import React,{Component} from "react";
import PropTypes from "prop-types"
import "./style.css"

export class NavBar extends Component{
    render(){
        const { children } = this.props
        console.log(children)
        
        return (
            <div className='nav-bar'>
                <div className="left">{children[0]}</div>
                <div className="center">{children[1]}</div>
                <div className="right">{children[2]}</div>
            </div>
        )
    }
} 

NavBar.propTypes = {
    childern:PropTypes.array
}

export default NavBar

这里再附上style.css文件:

h1,h2,body{
    margin: 0px;
    padding: 0px;
}

.nav-bar{
    display: flex;
    height: 40px;
    line-height: 40px;
    text-align: center;
}

.left,.right{
    width: 80px;
    background-color: #f00;
}

.center{
    flex:1;
    background-color: #f80;
}

这里我们通过childern来接收插槽内容,并通过children[0]children[1]children[2]来分别显示左侧、中间和右侧的内容。可以看到,NavBar组件的实现非常简单,通过children来接收插槽内容,然后通过{this.props.children}来显示内容。

NavBarTwo组件

我们再来看一下NavBarTwo组件的实现:

import React,{Component} from "react";

export class NavBarTwo extends Component{
    render(){
        const { leftSlot,centerSlot,rightSlot } = this.props
        
        return (
            <div className="nav-bar">
                <div className="left">{leftSlot}</div>
                <div className="center">{centerSlot}</div>
                <div className="right">{rightSlot}</div>
            </div>
        )
    }
} 

export default NavBarTwo

这里我们通过props来接收插槽内容,然后通过{this.props.leftSlot}{this.props.centerSlot}{this.props.rightSlot}来分别显示左侧、中间和右侧的内容。可以看到,NavBarTwo组件的实现也非常简单,通过props来接收插槽内容,然后通过{this.props.children}来显示内容。

本文总结

本文主要介绍了React中插槽的实现,通过children属性和props来实现插槽。通过本文的介绍,相信大家已经对React中插槽的实现有了更深入的了解。这里我们就会发现React的灵活之处了吧!最后我们附上效果图:

React中组件的插槽实现

感谢大家观看,我们下次见

评论(0)

最新评论

  • See details

    of course like your web site but you need to test the spelling on quite a few of your posts. A number of them are rife with spelling problems and I in finding it very troublesome to inform the reality nevertheless I will surely come again again.

  • 晚夜

    新年快乐!

  • abzzp

    十天看一部剧,还可以吧[[呲牙]]

  • ab

    @梦不见的梦 行,谢谢提醒,我优化一下

  • 梦不见的梦

    网站的速度有待提升,每次打开都要转半天还进不来呢

  • abzzp

    @React实战爱彼迎项目(二) - 程序员鸡皮 哪里有问题了,报错了吗?[[微笑]]

  • abzzp

    @Teacher Du 那是怕你们毕不了业,我大学那会儿给小礼品[[发呆]]

  • Teacher Du

    我们大学那会,献血还给学分~

  • @ab 我想去学网安,比如网警,但分也贼高😕

  • ab

    @夜 加油,你一样也可以成为程序员的,需要学习资料可以V我

日历

2025年04月

  12345
6789101112
13141516171819
20212223242526
27282930   

文章目录

推荐关键字: React vue JavaScript Golang 观后感 ES6 SEO 读后感

站点公告
本站是作为记录一名北漂程序员编程学习以及日常的博客,欢迎添加微信BmzhbjzhB咨询交流......
点击小铃铛关闭
配色方案