当前位置:网站首页>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;};
边栏推荐
- Etcd learning
- 数据湖(十六):Structured Streaming实时写入Iceberg
- Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
- tiny-emitter.js:一个小型的事件订阅发布库
- [wechat applet] label (86/100)
- MCU experiment record
- HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
- 教你如何定位不合理的SQL?并优化之
- Compile ue5.0
- OA and fansoft Bi cross system users, departments and posts synchronous summary
猜你喜欢

Leetcode55. Jumping game

实战|记一次攻防演练打点

The 6th "Blue Hat Cup" National College Students' Cyber Security Skills Competition writeup

教你如何定位不合理的SQL?并优化之

Androd releases jitpack open source project (gradle7.2)

Salt and ice particles cannot be distinguished

Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life

Data link layer protocol -- Ethernet protocol

5年经验的大厂测试/开发程序员,怎样突破技术瓶颈?大厂通病......

Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?
随机推荐
Introduction to fundamentals of operations research [1]
2022-7-18 summary
Ownership in rust -- introduction of rust language Xiaobai 11
Gbase JDBC connection database exception
Luogu p4281 [ahoi2008] emergency gathering / gathering solution
Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
mitt.js:小型事件发布订阅库
Document collaboration tool recommendation
看问题的角度
ESWC 2018 | R-GCN:基于图卷积网络的关系数据建模
OA and fansoft Bi cross system users, departments and posts synchronous summary
Learn to use PHP to get the URL address link after resetting
Summary of UPR optimization suggestions of unity
很多时候都是概率
[wechat applet] label (86/100)
Introduction to CpG control network
[live review] AI customer service "changes according to the situation", and man-machine dialogue can be easier
Etcd learning
【微信小程序】拍卖商品详情页设计与交互实现(包含倒计时、实时更新出价)
Analysis of lottery winning numbers in history