diff --git a/src/hooks.ts b/src/hooks.ts index 170b4e69..d755049a 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -1,11 +1,16 @@ +import { useMemo } from 'react'; + import AsyncStorage from './AsyncStorage'; -import type { AsyncStorageHook } from './types'; + +import type { AsyncStorageHook, Callback, CallbackWithResult } from './types'; export function useAsyncStorage(key: string): AsyncStorageHook { - return { - getItem: (...args) => AsyncStorage.getItem(key, ...args), - setItem: (...args) => AsyncStorage.setItem(key, ...args), - mergeItem: (...args) => AsyncStorage.mergeItem(key, ...args), - removeItem: (...args) => AsyncStorage.removeItem(key, ...args), - }; + const asyncStorage = useMemo(() => ({ + getItem: (callback?: CallbackWithResult) => AsyncStorage.getItem(key, callback), + setItem: (value: string, callback?: Callback) => AsyncStorage.setItem(key, value, callback), + mergeItem: (value: string, callback?: Callback) => AsyncStorage.mergeItem(key, value, callback), + removeItem: (callback?: Callback) => AsyncStorage.removeItem(key, callback), + }), [key]); + + return asyncStorage; }