Fix: jotai storage initial value

This commit is contained in:
Dreamacro 2023-02-28 11:57:54 +08:00
parent 15fb59681f
commit 0f141b679c

View File

@ -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<Array<{ export const hostsStorageAtom = atomWithStorage<Array<{
hostname: string hostname: string
port: string port: string
secret: string secret: string
}>>('externalControllers', []) }>>('externalControllers', JSON.parse(hostsStorageOrigin))
export const hostSelectIdxStorageAtom = atomWithStorage<number>('externalControllerIndex', 0) export const hostSelectIdxStorageAtom = atomWithStorage<number>('externalControllerIndex', parseInt(hostSelectIdxStorageOrigin))
export function useAPIInfo () { export function useAPIInfo () {
const clashx = useAtomValue(clashxConfigAtom) const clashx = useAtomValue(clashxConfigAtom)