假設(shè)我們定義一個react組件,想要在react組件中像vue那樣傳入插槽內(nèi)容。因為,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>)
}
像上面這種方式,就實現(xiàn)了vue的slot插槽。