一. 使用children实现插槽
- 直接在组件中插入children
- 在目标组件拿到插入的内容进行展示
import React, { Component } from 'react'
import NavBar from './nav-bar'
export class App extends Component {
render() {
const btn = <button>按钮2</button>
return (
<div>
{/* 1.使用children实现插槽 */}
<NavBar>
<button>按钮</button>
<h2>哈哈哈</h2>
<i>斜体文本</i>
</NavBar>
)
}
}
export default App
import React, { Component } from 'react'
// import PropTypes from "prop-types"
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 = {
// children: PropTypes.array
// }
export default NavBar
打印console.log(children)如下:
二. 使用props实现插槽
通过children实现方案虽然可行,但是有如下缺点:
- 通过索引值传入的元素很容易出错,不能精准获取
- 如果只有一个插槽,
children打印显示的是字符串
export class App extends Component {
render() {
const btn = <button>按钮2</button>
return (
<div>
{/* 2.使用props实现插槽 */}
<NavBarTwo
leftSlot={btn}
centerSlot={<h2>呵呵呵</h2>}
rightSlot={<i>斜体2</i>}
/>
</div>
)
}
}
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
三. 作用域插槽
作用域插槽是带数据的插槽,子组件提供给父组件的参数,父组件根据子组件传过来的插槽数据来进行不同的展现和填充内容。
import React, { Component } from 'react'
import TabControl from './TabControl'
export class App extends Component {
constructor() {
super()
this.state = {
titles: ["流行", "新款", "精选"],
tabIndex: 0
}
}
tabClick(tabIndex) {
this.setState({ tabIndex })
}
//获取子组件传来的参数
getTabItem(item) {
if (item === "流行") {
return <span>{item}</span>
} else if (item === "新款") {
return <button>{item}</button>
} else {
return <i>{item}</i>
}
}
render() {
const { titles, tabIndex } = this.state
return (
<div className='app'>
<TabControl
titles={titles}
tabClick={i => this.tabClick(i)}
itemType={item => this.getTabItem(item)}
/>
<h1>{titles[tabIndex]}</h1>
</div>
)
}
}
export default App
import React, { Component } from 'react'
import "./style.css"
export class TabControl extends Component {
constructor() {
super()
this.state = {
currentIndex: 0
}
}
itemClick(index) {
// 1.自己保存最新的index
this.setState({ currentIndex: index })
// 2.让父组件执行对应的函数
this.props.tabClick(index)
}
render() {
const { titles, itemType } = this.props
const { currentIndex } = this.state
return (
<div className='tab-control'>
{
titles.map((item, index) => {
return (
<div
className={`item ${index === currentIndex?'active':''}`}
key={item}
onClick={e => this.itemClick(index)}
>
{itemType(item)}
</div>
)
})
}
</div>
)
}
}
export default TabControl