UNPKG

react-native-mmkv

Version:

⚡️ The fastest key/value storage for React Native.

34 lines (33 loc) 1.09 kB
import { useState } from 'react'; import { getDefaultMMKVInstance } from '../createMMKV/getDefaultMMKVInstance'; import { useMMKVListener } from './useMMKVListener'; /** * Get a list of all keys that exist in the given MMKV {@linkcode instance}. * The keys update when new keys are added or removed. * @param instance The instance to listen to changes to (or the default instance) * * @example * ```ts * useMMKVKeys(instance) * ``` */ export function useMMKVKeys(instance) { const mmkv = instance ?? getDefaultMMKVInstance(); const [allKeys, setKeys] = useState(() => mmkv.getAllKeys()); useMMKVListener((key) => { // a key changed setKeys((keys) => { const currentlyHasKey = keys.includes(key); const hasKey = mmkv.contains(key); if (hasKey !== currentlyHasKey) { // Re-fetch the keys from native return mmkv.getAllKeys(); } else { // We are up-to-date. return keys; } }); }, mmkv); return allKeys; }