react实现默认插槽以及具名插槽

学过vue的话,对插槽并不陌生,react的插槽和vue的有几分类似

举例1-匿名插槽

Father.jsx

import React,{Component, Suspense} from 'react'
import Children from './Children'
export default class Father extends Component{
    constructor(props){
        super(props)
        this.state={
        }
    }
    render (){
        return (
            <div>
          <Children>
                    <div onClick={()=>console.log('ppp')}>
                      <p>p1p1p1p1p1p1pp1p1p11p1pp11</p>
                    </div>
                    <div>
                      <h2>h2h2h2h2h2hh2h2</h2>
                    </div>
                  </Children>
            </div>
        )
    }
}

Children.jsx

import React,{Component} from 'react'
import Grandson from './Grandson'
import { ThemeContext,UserContext } from "./Father";//引入父组件的Consumer容器
export default class Children extends Component{
    constructor(props){
        super(props)
        this.state={

        }
    }
    render (){
        return (
            <div>
               {
                Array.isArray(this.props.children) ? this.props.children.map((item,index)=>{
                  return item
                }) : this.props.children
              }
            </div>
        )
    }
}

以上react代码就像是vue的匿名插槽,但是react中,被组件包裹的子标签(孙及以下不算),每一个都会在组件中的this.props.children中,只有一个的话this.props.children为单个元素对象,多个的话为数组

举例2-具名插槽

Father.jsx

import React,{Component, Suspense} from 'react'
import Children from './Children'
export default class Father extends Component{
    constructor(props){
        super(props)
        this.state={
        }
    }
    render (){
        return (
            <div>
          <Children>
                    <div slot="pView">
                      <p>p1p1p1p1p1p1pp1p1p11p1pp11</p>
                    </div>
                    <div slot="hView">
                      <h2>h2h2h2h2h2hh2h2</h2>
                    </div>
                  </Children>
            </div>
        )
    }
}

Children.jsx

import React,{Component} from 'react'
import Grandson from './Grandson'
import { ThemeContext,UserContext } from "./Father";//引入父组件的Consumer容器
export default class Children extends Component{
    constructor(props){
        super(props)
        this.state={

        }
    }
    render (){
	let children = Array.isArray(this.props.children) ? this.props.children : [this.props.children];
      const slots = children.reduce((slots,item)=>{
        slots[item.props.slot] = item
        return slots
      }, {})
        return (
            <div>
               {slots['hView']}
			   {slots['pView']}
            </div>
        )
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值