Just be really sure it's set
This commit is contained in:
+12
-1
@@ -1,4 +1,4 @@
|
|||||||
import {useCallback, useEffect, useState} from 'react'
|
import {useCallback, useEffect, useState, useRef} from 'react'
|
||||||
import {MMKV} from 'react-native-mmkv'
|
import {MMKV} from 'react-native-mmkv'
|
||||||
|
|
||||||
import {type Account, type Device} from '#/storage/schema'
|
import {type Account, type Device} from '#/storage/schema'
|
||||||
@@ -112,10 +112,21 @@ export function useStorage<
|
|||||||
(data: StorageSchema<Store>[Key]) => void,
|
(data: StorageSchema<Store>[Key]) => void,
|
||||||
] {
|
] {
|
||||||
type Schema = StorageSchema<Store>
|
type Schema = StorageSchema<Store>
|
||||||
|
const initialized = useRef(false)
|
||||||
const [value, setValue] = useState<Schema[Key] | undefined>(() =>
|
const [value, setValue] = useState<Schema[Key] | undefined>(() =>
|
||||||
storage.get(scopes),
|
storage.get(scopes),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!initialized.current) {
|
||||||
|
const raw = storage.get(scopes)
|
||||||
|
if (value !== raw) {
|
||||||
|
initialized.current = true
|
||||||
|
setValue(raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [value, scopes])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const sub = storage.addOnValueChangedListener(scopes, () => {
|
const sub = storage.addOnValueChangedListener(scopes, () => {
|
||||||
if (debug) logger.debug(`${debug} - storage change`, {
|
if (debug) logger.debug(`${debug} - storage change`, {
|
||||||
|
|||||||
Reference in New Issue
Block a user