diff --git a/src/store/jotai/utils.ts b/src/store/jotai/utils.ts index 5c7ac51..2fefca9 100644 --- a/src/store/jotai/utils.ts +++ b/src/store/jotai/utils.ts @@ -41,15 +41,6 @@ export const createLocalStorageAtom = ( // 하이드레이션(저장소 주입)과 사용자 set 경합 방지용 플래그 let wasOverridden = false; // 마운트 이후 사용자가 값을 쓴 적이 있는지 - baseAtom.onMount = (setAtom) => { - if (typeof window === "undefined" || wasOverridden) return; - - const stored = getLocalStorage(key); - if (stored && typeof stored === "object") { - setAtom(stored as T); - } - }; - const persistentAtom = atom( (get) => get(baseAtom), (get, set, update: T | ((prev: T) => T)) => { @@ -74,6 +65,15 @@ export const createLocalStorageAtom = ( }, ); + persistentAtom.onMount = (setAtom) => { + if (typeof window === "undefined" || wasOverridden) return; + + const stored = getLocalStorage(key); + if (stored && typeof stored === "object") { + setAtom(stored as T); + } + }; + return persistentAtom; } @@ -89,15 +89,6 @@ export const createSessionStorageAtom = ( const realInitialValue = loadInitialState("session", key, initialValue); const baseAtom: PrimitiveAtom = atom(realInitialValue); - baseAtom.onMount = (setAtom) => { - if (typeof window === "undefined") return; - - const stored = getSessionStorage(key); - if (stored && typeof stored === "object") { - setAtom(stored as T); - } - }; - const persistentAtom = atom( (get) => get(baseAtom), (get, set, update: T | ((prev: T) => T)) => { @@ -122,5 +113,14 @@ export const createSessionStorageAtom = ( }, ); + persistentAtom.onMount = (setAtom) => { + if (typeof window === "undefined") return; + + const stored = getSessionStorage(key); + if (stored && typeof stored === "object") { + setAtom(stored as T); + } + }; + return persistentAtom; }