当前位置:网站首页>DOM在Ahooks中的处理过程
DOM在Ahooks中的处理过程
2022-07-25 04:40:00 【nginx】
const createEffectWithTarget = (useEffectType: typeof useEffect | typeof useLayoutEffect) => {/*** @param effect* @param deps* @param target target should compare ref.current vs ref.current, dom vs dom, ()=>dom vs ()=>dom*/const useEffectWithTarget = (effect: EffectCallback,deps: DependencyList,target: BasicTarget| BasicTarget[],) => {const hasInitRef = useRef(false);const lastElementRef = useRef<(Element | null)[]>([]);const lastDepsRef = useRef([]);const unLoadRef = useRef();// useEffect 或者 useLayoutEffectuseEffectType(() => {// 处理 DOM 目标元素const targets = Array.isArray(target) ? target : [target];const els = targets.map((item) => getTargetElement(item));// init run// 首次初始化的时候执行if (!hasInitRef.current) {hasInitRef.current = true;lastElementRef.current = els;lastDepsRef.current = deps;// 执行回调中的 effect 函数unLoadRef.current = effect();return;}// 非首次执行的逻辑if (// 目标元素或者依赖发生变化els.length !== lastElementRef.current.length ||!depsAreSame(els, lastElementRef.current) ||!depsAreSame(deps, lastDepsRef.current)) {// 执行上次返回的结果unLoadRef.current?.();// 更新lastElementRef.current = els;lastDepsRef.current = deps;unLoadRef.current = effect();}});useUnmount(() => {// 卸载unLoadRef.current?.();// for react-refreshhasInitRef.current = false;});};return useEffectWithTarget;};
边栏推荐
- PyG搭建GCN实现链接预测
- How to get the database creation time?
- Json.tojsonstring cannot pass Boolean
- QT download installation tutorial
- [sht30 temperature and humidity display based on STM32F103]
- 看问题的角度
- Analysis of lottery winning numbers in history
- SMIC's revenue in 2018 was $3.36 billion, and 14nm technology was mass produced this year
- Ffmpeg download and installation
- 推荐系统-协同过滤在Spark中的实现
猜你喜欢

Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?

How can test / development programmers with 5 years of experience break through the technical bottleneck? Common problems in big factories

Zhongchuang computing power won the recognition of "2022 technology-based small and medium-sized enterprises"
![[sht30 temperature and humidity display based on STM32F103]](/img/43/bbc66ab2d56cfa9dc05d795e8fe456.jpg)
[sht30 temperature and humidity display based on STM32F103]

How to publish your own NPM package

Summary and Prospect of aut, the transport layer protocol of sound network -- dev for dev column
![[ CTF 学习 ] CTF 中的隐写集合 —— 图片隐写术](/img/32/2da78bd5866cfab9ee64dfcb1c1204.png)
[ CTF 学习 ] CTF 中的隐写集合 —— 图片隐写术

This low code reporting tool is needed for data analysis

Implementation of recommendation system collaborative filtering in spark

When we talk about immutable infrastructure, what are we talking about
随机推荐
Pyg builds GCN to realize link prediction
Salt and ice particles cannot be distinguished
Openworm project compilation
Actual combat | record an attack and defense drill management
How to transfer NFT metadata from IPFs to smart contracts
I didn't expect Mysql to ask these questions
【微信小程序】拍卖商品详情页设计与交互实现(包含倒计时、实时更新出价)
Logu p3398 hamsters find sugar solution
[analysis of GPIO register (crl/crh) configuration of STM32]
When we talk about immutable infrastructure, what are we talking about
C# 之 FileStream类介绍
85 distributed project construction
深入掌握Service
# 1. Excel的IF函数
ES6 -- Methods and extensions of array objects, traversal of arrays, and extension methods of strings
Baklib: share some methods about building enterprise knowledge management (km)
Ffmpeg download and installation
IT自媒体高调炫富,被黑客组织盯上,铁定要吃牢饭了…
@ResponseBody注解的总结
读书的思考