From 0f141b679cb5d623281589444fd71a03c07a2928 Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+Dreamacro@users.noreply.github.com> Date: Tue, 28 Feb 2023 11:57:54 +0800 Subject: [PATCH] Fix: jotai storage initial value --- src/stores/request.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/stores/request.ts b/src/stores/request.ts index 0a8954c..9f8a106 100644 --- a/src/stores/request.ts +++ b/src/stores/request.ts @@ -19,12 +19,16 @@ const clashxConfigAtom = atom(async () => { } }) +// jotai v2 use initialValue first avoid hydration warning, but we don't want that +const hostsStorageOrigin = localStorage.getItem('externalControllers') ?? '[]' +const hostSelectIdxStorageOrigin = localStorage.getItem('externalControllerIndex') ?? '0' + export const hostsStorageAtom = atomWithStorage>('externalControllers', []) -export const hostSelectIdxStorageAtom = atomWithStorage('externalControllerIndex', 0) +}>>('externalControllers', JSON.parse(hostsStorageOrigin)) +export const hostSelectIdxStorageAtom = atomWithStorage('externalControllerIndex', parseInt(hostSelectIdxStorageOrigin)) export function useAPIInfo () { const clashx = useAtomValue(clashxConfigAtom)