
举个例子,假设我们有一个组件,它可以呈现一张在屏幕上追逐鼠标的猫的图片。我们或许会使用 <Mouse> 组件,它能够动态决定什么需要渲染的,而不是将 <Cat> 硬编码到 <Mouse>组件里,并有效地改变它的渲染结果。
// cat.jsimport catImage from './cat.jpg'class Cat extends Taro.Component {static defaultProps = {mouse: {x: 0,y: 0}}render() {const { mouse } = this.props;return (<Image src={catImage} style={{ position: 'absolute', left: mouse.x, top: mouse.y }} />);}}// mouse.jsclass Mouse extends Taro.Component {constructor(props) {super(props);this.handleMouseMove = this.handleClick.bind(this);this.state = { x: 0, y: 0 };}handleClick(event) {const { x, y } = event.detailthis.setState({x,y});}render() {return (<View style={{ height: '100%' }} onClick={this.handleClick}>{/*我们可以把 prop 当成一个函数,动态地调整渲染内容。*/}{this.props.renderCat(this.state)}</View>);}}// MouseTracker.jsclass MouseTracker extends Taro.Component {render() {return (<View><View>点击鼠标!</View>{/*Mouse 如何渲染由 MouseTracker 的状态控制*/}<Mouse renderCat={mouse => (<Cat mouse={mouse} />)}/></View>);}}
现在,我们提供了一个 render 方法 让 <Mouse> 能够动态决定什么需要渲染,而不是克隆 <Mouse> 组件然后硬编码来解决特定的用例。
更具体地说,render prop 是一个用于告知组件需要渲染什么内容的函数 prop。
这项技术使我们共享行为非常容易。要获得这个行为,只要渲染一个带有 render prop 的 <Mouse> 组件就能够告诉它当前鼠标坐标 (x, y) 要渲染什么。
注意事项
render props 是基于小程序 slot 机制实现的。 因此它受到的限制和 children 与组合的限制一样多,更多详情请查看文档Children与组合 。
