Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 16x 16x 16x 16x 16x 16x 14x 5x 5x 14x 7x 7x 16x 7x 7x 7x 7x 5x 2x 2x 2x 2x 7x 7x 16x 16x 7x 7x 7x 16x | /**
* 懒加载状态管理 Hook
* 用于管理组件的加载状态和延迟渲染
*/
import { useCallback, useEffect, useRef, useState } from 'react';
import { InteractionManager } from 'react-native';
interface UseLazyLoadOptions {
/** 延迟时间(毫秒) */
delay?: number;
/** 是否在交互完成后加载 */
waitForInteraction?: boolean;
/** 是否立即开始加载 */
immediate?: boolean;
}
interface UseLazyLoadReturn {
/** 是否准备好渲染 */
isReady: boolean;
/** 是否正在加载 */
isLoading: boolean;
/** 手动触发加载 */
startLoading: () => void;
/** 重置状态 */
reset: () => void;
}
/**
* 懒加载状态管理 Hook
*/
export function useLazyLoad(options: UseLazyLoadOptions = {}): UseLazyLoadReturn {
const { delay = 0, waitForInteraction = true, immediate = true } = options;
const [isReady, setIsReady] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const interactionRef = useRef<ReturnType<typeof InteractionManager.runAfterInteractions> | null>(
null
);
const cleanup = useCallback(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
if (interactionRef.current) {
interactionRef.current.cancel();
interactionRef.current = null;
}
}, []);
const startLoading = useCallback(() => {
cleanup();
setIsLoading(true);
const doLoad = () => {
if (delay > 0) {
timeoutRef.current = setTimeout(() => {
setIsReady(true);
setIsLoading(false);
}, delay);
} else {
setIsReady(true);
setIsLoading(false);
}
};
if (waitForInteraction) {
interactionRef.current = InteractionManager.runAfterInteractions(doLoad);
} else E{
doLoad();
}
}, [cleanup, delay, waitForInteraction]);
const reset = useCallback(() => {
cleanup();
setIsReady(false);
setIsLoading(false);
}, [cleanup]);
useEffect(() => {
Eif (immediate) {
startLoading();
}
return cleanup;
}, [immediate, startLoading, cleanup]);
return {
isReady,
isLoading,
startLoading,
reset,
};
}
// ==================== 批量懒加载 Hook ====================
interface UseBatchLazyLoadOptions {
/** 总数量 */
total: number;
/** 每批数量 */
batchSize?: number;
/** 批次间隔(毫秒) */
interval?: number;
}
interface UseBatchLazyLoadReturn {
/** 当前可见数量 */
visibleCount: number;
/** 是否全部加载完成 */
isComplete: boolean;
/** 加载进度 (0-1) */
progress: number;
/** 重置 */
reset: () => void;
}
/**
* 批量懒加载 Hook
* 用于长列表的渐进式渲染
*/
export function useBatchLazyLoad(options: UseBatchLazyLoadOptions): UseBatchLazyLoadReturn {
const { total, batchSize = 10, interval = 100 } = options;
const [visibleCount, setVisibleCount] = useState(Math.min(batchSize, total));
const intervalRef = useRef<ReturnType<typeof setTimeout> | null>(null);
useEffect(() => {
if (visibleCount >= total) return;
intervalRef.current = setTimeout(() => {
setVisibleCount((prev) => Math.min(prev + batchSize, total));
}, interval);
return () => {
if (intervalRef.current) {
clearTimeout(intervalRef.current);
}
};
}, [visibleCount, total, batchSize, interval]);
const reset = useCallback(() => {
setVisibleCount(Math.min(batchSize, total));
}, [batchSize, total]);
return {
visibleCount,
isComplete: visibleCount >= total,
progress: total > 0 ? visibleCount / total : 1,
reset,
};
}
// ==================== 视口懒加载 Hook ====================
interface UseViewportLazyLoadOptions {
/** 是否启用 */
enabled?: boolean;
/** 触发阈值(像素) */
threshold?: number;
}
/**
* 视口懒加载 Hook
* 当组件进入视口时触发加载
*/
export function useViewportLazyLoad(options: UseViewportLazyLoadOptions = {}) {
const { enabled = true, threshold = 100 } = options;
const [isVisible, setIsVisible] = useState(!enabled);
const [hasLoaded, setHasLoaded] = useState(!enabled);
const onViewableChange = useCallback(
(info: { viewableItems: { isViewable: boolean }[] }) => {
if (!enabled || hasLoaded) return;
const isNowVisible = info.viewableItems.some((item) => item.isViewable);
if (isNowVisible) {
setIsVisible(true);
setHasLoaded(true);
}
},
[enabled, hasLoaded]
);
return {
isVisible,
hasLoaded,
onViewableChange,
viewabilityConfig: {
viewAreaCoveragePercentThreshold: threshold,
},
};
}
|