Skip to content

Commit

Permalink
🐛 fix: 修正异步水合造成的初始状态不稳定的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Sep 9, 2023
1 parent 0b1391d commit 2208f8a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/store/session/hooks/useSessionHydrated.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { useState } from 'react';

import { useEffectAfterSessionHydrated } from '@/store/session';

import { useSessionStore } from '../store';
import { useOnFinishHydrationSession } from './useOnFinishHydrationSession';

export const useSessionHydrated = () => {
// 根据 sessions 是否有值来判断是否已经初始化
const hasInited = !!Object.values(useSessionStore.getState().sessions).length;

const [isInit, setInit] = useState(hasInited);

useOnFinishHydrationSession(() => {
useEffectAfterSessionHydrated(() => {
if (!isInit) setInit(true);
});
}, []);

return isInit;
};

0 comments on commit 2208f8a

Please sign in to comment.