当前位置:网站首页>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
边栏推荐
- 方舟:生存进化PVE模式和PVP模式
- 如何优雅的性能调优,分享一线大佬性能调优的心路历程
- 2022牛客多校联赛第五场 题解
- shell规范与变量
- ModuleNotFoundError: No module named 'yaml'
- 10 Practical Uses of NFTs (NFT System Development)
- The difference between groupByKey and reduceBykey
- Spark cluster construction
- C Pitfalls and pitfalls Appendix B Interview with Koenig and Moo
- 方舟生存进化是什么游戏?好不好玩
猜你喜欢

Based on php tourism website management system acquisition (php graduation design)

正则表达式

LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)

HCIP---多生成树协议相关知识点

365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号

LeetCode·32.最长有效括号·栈·动态规划

左旋氧氟沙星/载纳米雄黄磁性/As2O3磁性Fe3O4/三氧化二砷白蛋白纳米球

C语言必杀技3行代码把运行速度提升4倍

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

小程序--分包
随机推荐
RecycleView的使用
数字图像处理 第十二章——目标识别
Chapter 12, target recognition of digital image processing
scikit-learn no moudule named six
sizeof的详细解说和与strlen的区别
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.2 Early Experience of C Language
C语言必杀技3行代码把运行速度提升4倍
SQL injection of WEB penetration
你居然不懂Bitmap和Drawable? 相关知识大扫盲
多商户商城系统功能拆解19讲-平台端发票管理
AI应用第一课:支付宝刷脸登录
Spark集群搭建
shell规范与变量
牛血清白蛋白-葡聚糖-叶黄素纳米颗粒/半乳糖白蛋白磁性阿霉素纳米粒的制备
基于php动漫周边商城管理系统(php毕业设计)
AIDL通信
【力扣】字符串相乘
Flink cluster construction
Based on php online learning platform management system acquisition (php graduation design)
左旋氧氟沙星/载纳米雄黄磁性/As2O3磁性Fe3O4/三氧化二砷白蛋白纳米球