React study notes - Render Props
introduce
Render Props
Refers to值为函数
the technique of sharing code between React components using a prop- The component that has
render prop
accepts one返回React元素的函数
, and implements its own rendering logic by calling this function inside the component - Mainly used to extract logic for code reuse
use
Take Mouse & Cat
the example from the official documentation as an example
- Create
Mouse
a component, this component is only used to provide复用的状态逻辑代码
(state, method, etc.) - Will be exposed
复用的状态
asprops.render(state)
a method parameter to the outside of the component - Use
props.render()
the return value as the content to render
import React from 'react'
class Mouse extends React.Component {
// 省略操作方法
render(){
return this.props.render(this.state)
}
}
//根组件
export default class App extends React.Component {
render(){
<Mouse render={
mouse => (
<div>当前鼠标位置 {
mouse.x} {
mouse.y}<div>
)}/>
//此处的mouse参数实际上为Mouse组件的state,因为这个函数是在Mouse组件的render()中传参调用的
}
}
Note: It is not
render props
necessary to use therender
name of the modeprop
. You can name it whatever you wantprop
, it's just a received返回React元素的函数
oneprop
.
返回React元素的函数
In fact, the view is determined by the input from the parent component , and the life cycle functionMouse
in the componentrender()
just calls theprop
function passed in, and the function returns the view
Use children prop instead of render prop
You can also use the combination mode to achieve code reuse between components, which is similar to Slot
the concept in Vue
import React from 'react'
class Mouse extends React.Component {
// 省略操作方法
render(){
return this.props.children(this.state)
}
}
//根组件
export default class App extends React.Component {
render(){
<Mouse>
{
mouse => (
<div>当前鼠标位置 {
mouse.x} {
mouse.y}<div>
)}
</Mouse>
}
}
Since this technique requires passing a function to the component, it is recommended to children
type check the
import PropTypes from 'prop-types'
Mouse.propTypes = {
children: PropTypes.func.isRequired
}
Use Render props with React.PureComponent
Notice
If the function render
is created in the method, then render prop
usingReact.PureComponent
Because render()
a new function is created every time it is called for rendering, this will cause shallow comparisons to props
always prevProps === nextProps
befalse
class Mouse extends React.PureComponent {
// 与上面相同的代码......
}
class MouseTracker extends React.Component {
render() {
return (
<div>
<Mouse render={
mouse => (
<Cat mouse={
mouse} />
)}/>
</div>
);
}
}
In this example, since Mouse
the component's render prop
transported function is render()
defined in , this will cause MouseTracker
a new function to be generated each time Mouse
the component is rendered render prop
, thus negating React.PureComponent
the effect of inheriting from
solution
To solve this problem, you can define an instance method to passrender prop
class MouseTracker extends React.Component {
// 定义为实例方法,`this.renderTheCat`始终
// 当我们在渲染中使用它时,它指的是相同的函数
renderTheCat(mouse) {
return <Cat mouse={
mouse} />;
}
render() {
return (
<div>
<h1>Move the mouse around!</h1>
<Mouse render={
this.renderTheCat} />
</div>
);
}
}