当前位置:网站首页>Have you used useeffect and uselayouteffect
Have you used useeffect and uselayouteffect
2022-07-18 14:06:00 【baoleilei6】
Make a demand before , It is found that there is a secondary rendering . Record it here useEffect And useLayoutEffect The difference between
Let's start with a piece of code

What is the print order ? You guys can guess for yourself .
useEffect
The official definition of
Within the body of the function component ( This refers to React Rendering phase ) change DOM、 Add subscription 、 Set the timer 、 Logging and performing other operations with side effects are not allowed , Because it can lead to unexplained bug And destroy UI The consistency of .
To put it more generally
useEffect In fact, it is to make functional components have a life cycle .
useEffect Execution time of
The official definition of
And componentDidMount、componentDidUpdate The difference is , Pass to useEffect The function will complete the layout and drawing in the browser after , Called in a delay event . This makes it suitable for many common side effect scenarios , For example, setting subscription and event handling , Because most operations should not block the browser from updating the screen .
Generally speaking
No matter in the interview , Or at work . If someone asks you useEffect How to simulate the life cycle . You will answer without thinking
The second parameter passes an empty array , simulation
componentDidMountThe second parameter passes dependencies , simulation
componentDidUpdateThe second parameter passes an empty array , And through return To call a method in the form of , simulation
componentWillUnmount
however useEffect The execution order problem , Did you think of ? If the parent and child components exist at the same time useEffect Who left first ?
Look at a piece of code

result

Why are subcomponents useEffect Go ahead ?
analysis
We all deserve useEffect The execution of is in commit after ,React Of commit What does the stage do ? Simply speaking , Will be DOM Render to page . So can we think of ,useEffect In fact, after the page has been rendered , Triggered again ?
According to this execution logic :
The parent component enters
commitStage , Found to haveSonComponents need to be rendered .To begin
SonLife cycle of ,SonGet intocommitStage , Execute theuseEffect,SonEnd of renderingParent component
commitStage , Render completed , performuseEffect
useLayoutEffect
The official definition of
Its function signature and useEffect identical , But it will be in all DOM Synchronous call after change effect. You can use it to read DOM Layout and sync trigger re rendering . Before the browser does the drawing ,useLayoutEffect The internal update plan will be refreshed synchronously
Popular speaking
useLayoutEffect And componentDidMount It's synchronous . stay commit Before the stage , The execution order is better than useEffect

Execution order

useEffect And useLayoutEffect The difference between
useEffect Change value
benefitsDo not block the rendering update of the browser screenDisadvantageIt will cause secondary rendering , Poor user experience

Can't make a motion diagram .... Try it on your own ...
It'll flash , The first thing the page shows is Initial value , Click on Initial value , First change it to I changed , Then change to I changed again ? The user experience is very bad .
useLayoutEffect Change value
benefitsalong withcomponentDidMountSynchronous execution , It will not cause secondary renderingDisadvantageRendering updates that may block the browser screen

The first thing the page shows is Initial value , Click on Initial value , Change to I changed again
Execution diagram of the initial code

It may be a little understood . If you have a better understanding later, you will revise this article
边栏推荐
- Cultural tourism Night Tour: new opportunities for urban economic recovery and growth
- What points should be paid attention to in the selection of project management system?
- ping 命令还能这么玩?
- Common and practical SQL statements
- PHP QRCode生成二维码
- Idea merges dev branch code into master and so on
- T100自定义应用使用说明(azzi650)
- 高数 | 积分中值定理的开闭区间、积分第一中值定理及其推广
- base64编码解码原理和C语言实现
- 8、某网络拓扑如图所示,路由器R1通过接口E1、E2分别连接局域网1、局域网2,通过接口L0连接路由器R2,并通过路由器R2连接域名服务器与互联网。R1的L0接口的IP地址是202.118.2.1/2
猜你喜欢

Lianshengde w801- how to improve the efficiency of multi-channel ADC acquisition
![preg_replace 代码执行漏洞之[BJDCTF2020]ZJCTF,不过如此](/img/e4/29e07b21593bb5ff36cb1135fa1b45.png)
preg_replace 代码执行漏洞之[BJDCTF2020]ZJCTF,不过如此

关系图/族谱

Shuttle simulated rocket launch animation

【最全最详细】七种分布式全局 ID 生成策略

Interview problem: how to close an order without using a scheduled task?

基于JSP+Servlet的蜗居公寓租赁系统

文旅夜游:城市经济复苏增长新机遇

Dwelling apartment rental system based on jsp+servlet

MySQL common queries
随机推荐
[phantom engine UE] ue5 dynamic Pak resource loading method
Simulation volume leetcode [general] 1823 Find the winner of the game
T100debug operation record
Summary and induction: data fusion methods in deep neural networks
PMP每日一练 | 考试不迷路-7.15
Open source library musicplaymanager - encapsulate starrysky Music Library
Opencv:05 filter
npm与node版本不匹配 更新了npm 结果报错node和npm不匹配 怎么退回之前的npm 版本?或者说怎么查看node适配的npm版本
Two low-cost ways to attract users with integral check-in
Dwelling apartment rental system based on jsp+servlet
math_ Derivation of ordering inequality
Interface test practice - student information management system
Color supplement of MATLAB scientific research drawing (special part 6) - 336 traditional French colors
MySQL query data
积分签到吸引用户的两种低成本做法
MySQL common queries
二叉树的深度遍历
Depth traversal of binary tree
【Cityengine】Cityengine2019安装使用及城市模型构建
开源库MusicPlayManager - 封装StarrySky音乐库