pref: useMemo优化
This commit is contained in:
@@ -109,10 +109,13 @@ const [currentMode, setCurrentMode] = useState('vertical');
|
|||||||
'fourRowData.total': fourRowData.total,
|
'fourRowData.total': fourRowData.total,
|
||||||
});
|
});
|
||||||
|
|
||||||
// 根据模式选择数据源
|
// 根据模式选择数据源(使用 useMemo 缓存,避免重复计算)
|
||||||
// 纵向模式:data 是页码映射 { 1: [...], 2: [...] }
|
// 纵向模式:data 是页码映射 { 1: [...], 2: [...] }
|
||||||
// 平铺模式:data 是数组 [...]
|
// 平铺模式:data 是数组 [...]
|
||||||
const modeData = currentMode === 'four-row' ? fourRowData : verticalData;
|
const modeData = useMemo(
|
||||||
|
() => currentMode === 'four-row' ? fourRowData : verticalData,
|
||||||
|
[currentMode, fourRowData, verticalData]
|
||||||
|
);
|
||||||
const {
|
const {
|
||||||
data = currentMode === 'vertical' ? {} : [], // 纵向是对象,平铺是数组
|
data = currentMode === 'vertical' ? {} : [], // 纵向是对象,平铺是数组
|
||||||
loading = false,
|
loading = false,
|
||||||
@@ -123,9 +126,15 @@ const [currentMode, setCurrentMode] = useState('vertical');
|
|||||||
cachedPageCount = 0
|
cachedPageCount = 0
|
||||||
} = modeData;
|
} = modeData;
|
||||||
|
|
||||||
// 传递给 usePagination 的数据
|
// 传递给 usePagination 的数据(使用 useMemo 缓存,避免重复计算)
|
||||||
const allCachedEventsByPage = currentMode === 'vertical' ? data : undefined;
|
const allCachedEventsByPage = useMemo(
|
||||||
const allCachedEvents = currentMode === 'four-row' ? data : undefined;
|
() => currentMode === 'vertical' ? data : undefined,
|
||||||
|
[currentMode, data]
|
||||||
|
);
|
||||||
|
const allCachedEvents = useMemo(
|
||||||
|
() => currentMode === 'four-row' ? data : undefined,
|
||||||
|
[currentMode, data]
|
||||||
|
);
|
||||||
|
|
||||||
// 🔍 调试:选择的数据源
|
// 🔍 调试:选择的数据源
|
||||||
console.log('%c[DynamicNewsCard] 选择的数据源', 'color: #3B82F6; font-weight: bold;', {
|
console.log('%c[DynamicNewsCard] 选择的数据源', 'color: #3B82F6; font-weight: bold;', {
|
||||||
|
|||||||
Reference in New Issue
Block a user