当前位置:网站首页>render-props and higher order components
render-props and higher order components
2022-08-01 21:36:00 【like Feynman %】
1. Overview of React component reuse
- Think: What if some of the functions in two components are similar or the same?
- Processing method: reuse similar functions (associative function encapsulation)
- Reuse what?1.state 2.Method of operating state (component state logic)
- Two ways: 1. render props mode 2. Higher-order components (HOC)
- Note: These two methods are not new APIs, but a fixed pattern (writing method) evolved by using the coding skills of React's own characteristics.
2. Analysis of ideas
- idea: encapsulate the state to be reused and the method of operating the state into a component
- Question 1: How to get the state reused in the component
- When using a component, add a prop whose value is a function, and get it through the function parameter (requires internal implementation of the component)
- Question 2: How to render arbitrary UI
- Use the return value of this function as the UI content to be rendered (requires internal implementation of the component)
3. Steps to use
- Create a Mouse component and provide reused state logic code (1. state 2. method of operating state) in the component
- Use the state to be reused as the parameter of the props.render(state) method, exposed to the outside of the group
- Use the return value of props.render() as the content to render
4. Demonstrate the reuse of Mouse components

//The way the React scaffolding introduces picturesimport img from './images/open.png'class Mouse extends React.Component{state={x:0,y:0}handleMouseMouve= e=>{this.setState({x:e.clientX,//Get the mouse position in real timey:e.clientY,})}//listen for mouse movement eventscomponentDidMount(){window.addEventListener('mousemove',this.handleMouseMouve)}render(){return(this.props.render(this.state))}}class App extends React.Component{render(){return(render props mode
{//It feels a bit like a callback functionreturn(The position of the mouse: x:{mouse.x} y:{mouse.y}
)}}>{/* move image */}{return({position:'absolute',top:mouse.y-16,left:mouse.x-32}}/>)}}> )}}ReactDOM.render( ,document.getElementById('root'))5.children replace the render attribute (recommended)
- Note: It's not that the pattern is called render props and you must use a prop named render, in fact you can use any prop with any name
- The technique of making a prop a function and telling a component what to render is called the render props pattern
- Recommended: use children instead of the render attribute
//Usage in context:
{data=>data parameter indicates the received data---data}
class App extends React.Component{render(){return(render props mode
{mouse=>{return(mouse position:{mouse.x},{mouse.y}
)}} 6. Code optimization
- Recommended: add props validation to render props mode
- should unbind mousemove event on unmount
Mouse.propTypes={children:PropTypes.func.isRequired}//unbindcomponentWillUnmount(){window.removeEventListener('mousemove',this.handleMouseMouve)}
For propType errors, please refer to the article:http://t.csdn.cn/nQtzz
边栏推荐
- Day016 类和对象
- groupByKey和reduceBykey的区别
- ARFoundation入门教程U2-AR场景截图截屏
- LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)
- 方舟:生存进化官服和私服区别
- 基于php在线音乐网站管理系统获取(php毕业设计)
- Get started with Grafana in 15 minutes
- C Pitfalls and Defects Chapter 7 Portability Defects 7.11 An Example of a Portability Problem
- 关于npm的那些事儿
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.3 The Standard I/O Library and the C Preprocessor
猜你喜欢
随机推荐
Spark集群搭建
【Jmeter常用断言组件】
宝塔应用使用心得
关于npm的那些事儿
基于php影视资讯网站管理系统获取(php毕业设计)
Image fusion GANMcC study notes
C Pitfalls and Defects Chapter 5 Library Functions 5.5 Library Function Signal
Popular explanation: what is a clinical prediction model
数据库练习
基于php动漫周边商城管理系统(php毕业设计)
LeetCode·32.最长有效括号·栈·动态规划
Shell编程之条件语句
FusionGAN:A generative adversarial network for infrared and visible image fusion文章学习笔记
AIDL通信
C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion
Realize the superposition display analysis of DWG drawing with CAD in Cesium
scikit-learn no moudule named six
如何优雅的性能调优,分享一线大佬性能调优的心路历程
0DFS中等 LeetCode6134. 找到离给定两个节点最近的节点
【力扣】字符串相乘








