美文网首页
在react上实现vue的插槽slot

在react上实现vue的插槽slot

作者: 阿凡农 | 来源:发表于2021-06-25 16:30 被阅读0次

    假设我们定义一个react组件,想要在react组件中像vue那样传入插槽内容。因为,react中一切都是js,插槽可以通过props传递进来并渲染。那么,可以使用组件的props来传递:
    function child(props){
    return <div>{poops.slotA}</div>
    }
    使用组件的时候:
    import child from 'path/to/child'

    function slotDom = function(){return <span>我是插槽部分</span>}

    function father(){
    return (<div>
    <child slotA={<slotDom />}>
    </div>)
    }

    像上面这种方式,就实现了vue的slot插槽。

    相关文章

      网友评论

          本文标题:在react上实现vue的插槽slot

          本文链接:https://www.haomeiwen.com/subject/fxsoyltx.html