From c9e68caab1f7d92cdb27f617ded4d7e6a3e2a860 Mon Sep 17 00:00:00 2001 From: vvwn <39298624+vvwn@users.noreply.github.com> Date: Tue, 24 Feb 2026 15:34:28 +0800 Subject: [PATCH] Refactor subscription logic for Zustand v4/v5 --- components/AutoSync.tsx | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/components/AutoSync.tsx b/components/AutoSync.tsx index a81fc20..a64ddb4 100644 --- a/components/AutoSync.tsx +++ b/components/AutoSync.tsx @@ -25,18 +25,17 @@ export function AutoSync() { // 1. 刚打开网页时,主动从云端拉取一次最新数据 pullFromCloud(); - // 2. 监听本地数据的变化,如果用户看了新视频或收藏了,延迟 5 秒后推送到云端 + // 2. 监听本地数据的变化,如果数据变了,延迟 5 秒后推送到云端 const debouncedPush = debounce(pushToCloud, 5000); - const unsubHistory = useHistoryStore.subscribe( - (state) => state.viewingHistory, - () => debouncedPush() - ); + // 修改点在这里:Zustand v4/v5 默认 subscribe 只接受一个参数 + const unsubHistory = useHistoryStore.subscribe(() => { + debouncedPush(); + }); - const unsubFavorites = useFavoritesStore.subscribe( - (state) => state.favorites, - () => debouncedPush() - ); + const unsubFavorites = useFavoritesStore.subscribe(() => { + debouncedPush(); + }); return () => { unsubHistory();