8-React如何实现slot

/*
 * @Description: 
 * @Version: 2.0
 * @Autor: sun
 * @Date: 2022-10-16 00:14:12
 * @LastEditTime: 2022-10-16 00:33:54
 */
import React, { Component } from 'react'

import NavBar from './NavBar'
import NavBar2 from './NavBar2'
import './style.css'

export default class App extends Component {
  render() {
    return (
      <div>
        <NavBar>
          <strong>aaa</strong>
          <strong>bbb</strong>
          <strong>ccc</strong>
        </NavBar>

        <NavBar2
          leftSlot={ <strong>aaa</strong> }
          centerSlot={ <strong>bbb</strong> }
          rightSlot={ <strong>ccc</strong> }
        />
      </div>
    )
  }
}

 

/*
 * @Description: 
 * @Version: 2.0
 * @Autor: sun
 * @Date: 2022-10-16 00:14:12
 * @LastEditTime: 2022-10-16 00:28:53
 */
import React, { Component } from 'react';

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

export default NavBar;

 

 

/*
 * @Description: 
 * @Version: 2.0
 * @Autor: sun
 * @Date: 2022-10-16 00:14:12
 * @LastEditTime: 2022-10-16 00:31:35
 */
import React, { Component } from 'react';

class NavBar2 extends Component {
  render() {

    const { leftSlot, centerSlot, rightSlot } = this.props

    return (
      <div className='nav-bar'>
        <div className='nav-item nav-left'>
          { leftSlot }
        </div>
        <div className='nav-item nav-center'>
          { centerSlot }
        </div>
        <div className='nav-item nav-right'>
          { rightSlot }
        </div>
      </div>
    );
  }
}

export default NavBar2;

 

body {
  margin: 0;
  padding: 0;
}

.nav-bar {
  display: flex;
}

.nav-item {
  height: 44px;
  line-height: 44px;
  text-align: center;
}

.nav-left, .nav-right {
  width: 70px;
  background: red;
}

.nav-center {
  flex: 1;
  background-color: blue;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React 中没有内置的具名插槽和作用域插槽的概念,但可以使用 props 和组件的嵌套来实现类似的功能。 具名插槽可以通过创建一个父组件并在其中定义多个子组件来实现。父组件可以使用 props 将数据传递给子组件,并在子组件中根据需要渲染。以下是一个示例: ```jsx // 父组件 function ParentComponent() { return ( <div> <ChildComponent name="slot1"> <div>内容1</div> </ChildComponent> <ChildComponent name="slot2"> <div>内容2</div> </ChildComponent> </div> ); } // 子组件 function ChildComponent({ name, children }) { return ( <div className={`slot-${name}`}> {children} </div> ); } ``` 在上面的示例中,父组件 `ParentComponent` 创建了两个 `ChildComponent` 子组件,并通过 `name` 属性进行区分。子组件根据传入的 `name` 渲染不同的内容。 作用域插槽可以通过将函数作为子组件传递给父组件来实现。父组件可以调用该函数,并将数据作为参数传递给该函数。以下是一个示例: ```jsx // 父组件 function ParentComponent() { return ( <div> <ScopedSlotComponent> {(data) => ( <div>作用域插槽内容: {data}</div> )} </ScopedSlotComponent> </div> ); } // 子组件 function ScopedSlotComponent({ children }) { const data = "作用域插槽数据"; return children(data); } ``` 在上面的示例中,`ParentComponent` 使用 `ScopedSlotComponent` 组件,并将一个函数作为子组件传递给它。`ScopedSlotComponent` 调用该函数,并将数据作为参数传递给它,然后将返回的 JSX 渲染出来。 这样就可以实现类似于具名插槽和作用域插槽的效果。请注意,这只是一种基于 React实现方式,不同的框架可能有不同的实现方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值