当前位置:网站首页>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
边栏推荐
猜你喜欢

第一讲 测试知多少

The difference between groupByKey and reduceBykey

教你VSCode如何快速对齐代码、格式化代码

Based on php animation peripheral mall management system (php graduation design)

如何优雅的性能调优,分享一线大佬性能调优的心路历程

ModuleNotFoundError: No module named 'yaml'

HCIP---企业网的架构

TP5-NPs负载噻吩类化合物TP5白蛋白纳米粒/阿魏酸钠新糖牛血清蛋白纳米粒

包含吲哚菁绿的多聚体白蛋白纳米球/载马钱子碱纳米粒的牛血清白蛋白微球的制备

MySQL相关知识
随机推荐
用户量大,Redis没法缓存响应,数据库宕机?如何排查解决?
File operations of WEB penetration
【Unity实战100例】文件压缩Zip和ZIP文件的解压
2022牛客多校联赛第五场 题解
Based on php tourism website management system acquisition (php graduation design)
牛血清白蛋白-葡聚糖-叶黄素纳米颗粒/半乳糖白蛋白磁性阿霉素纳米粒的制备
groupByKey和reduceBykey的区别
Image fusion GANMcC study notes
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.3 The Standard I/O Library and the C Preprocessor
Pagoda application experience
Based on php animation peripheral mall management system (php graduation design)
property语法
shell specification and variables
JSD - 2204 - Knife4j framework - processing - Day07 response results
Spark shuffle tuning
数字图像处理 第十二章——目标识别
Appendix A printf, varargs and stdarg a. 2 use varargs. H to realize the variable argument list
365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses
网络水军第一课:手写自动弹幕
scikit-learn no moudule named six