[React] useLayoutEffect
useLayoutEffect
is almost the same as useEffect
except that it's synchronous to render as opposed to scheduled like useEffect
is. If you're migrating from a class component to a hooks-using function component, this can be helpful too because useLayoutEffect
runs at the same time as componentDidMount
and componentDidUpdate
whereas useEffect
is scheduled after. This should be a temporary fix.
The only time you should be using useLayoutEffect
is to measure DOM nodes for things like animations. In the example, I measure the textarea after every time you click on it (the onClick is to force a re-render.) This means you're running render twice but it's also necessary to be able to capture the correct measurments.
If you make the useLayoutEffect
into a useEffect
it will have a janky re-render where it'll flash before it renders correctly. This is exactly why we need useLayoutEffect
.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-12-09 [Web] Possible bug for Cache the network request
2020-12-09 [Javascript] Broadcaster + Operator + Listener pattern -- 25. Save Network Requests by Using a Cache
2020-12-09 [Java Spring] Aspect
2019-12-09 [Algorithm] 242. Valid Anagram
2019-12-09 [Algorithm] 155. Min Stack
2017-12-09 [Python] Format Strings in Python
2017-12-09 [Python] Execute a Python Script