当前位置:网站首页>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;};
边栏推荐
- [wechat applet] picker scroll selector (85/100)
- 【基于stm32f103的SHT30温湿度显示】
- Get the parameters of the browser address bar
- Zhongchuang computing power won the recognition of "2022 technology-based small and medium-sized enterprises"
- [CTF learning] steganography set in CTF -- picture steganography
- 推荐系统-协同过滤在Spark中的实现
- 数据湖(十六):Structured Streaming实时写入Iceberg
- Gbase JDBC connection database exception
- Database design process
- Idea2021 installation
猜你喜欢

Druid connection pool - strong self-study from 0. Those who don't understand Druid can click in. If you know not to click in, you will think I'm wordy

Teach you how to locate unreasonable SQL? And optimize it

Getting started with scratch

Summary of UPR optimization suggestions of unity

It we media shows off its wealth in a high profile, and is targeted by hacker organizations. It is bound to be imprisoned

Introduction to CpG control network

If you don't know these 20 classic redis interview questions, don't go to the interview!

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

ESWC 2018 | r-gcn: relational data modeling based on graph convolution network

HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier
随机推荐
Document collaboration tool recommendation
@ResponseBody注解的总结
[CTF learning] steganography set in CTF -- picture steganography
How to test data in the process of data warehouse migration?
教你如何定位不合理的SQL?并优化之
深入掌握Service
市场的调节
C# 之 FileStream类介绍
STM32 Development Notes 118: using CMSIS DSP Library in stm32cube IDE
Purpose of setting novice task period in the integral system
自然的状态最好
Gbase JDBC connection database exception
HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
Burpsuite爆破之token值替换
Why does the official not recommend using UUID as MySQL primary key
SMIC's revenue in 2018 was $3.36 billion, and 14nm technology was mass produced this year
tiny-emitter.js:一个小型的事件订阅发布库
Analysis of lottery winning numbers in history
Zhongchuang computing power won the recognition of "2022 technology-based small and medium-sized enterprises"
1. If function of Excel