forked from kevinresol/react-native-default-preference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useDefaultPreference.ts
53 lines (42 loc) · 1.34 KB
/
useDefaultPreference.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { useEffect } from 'react';
import DefaultPreference from './index';
const useDefaultPreference = (groupName: string = 'default') => {
useEffect(() => {
DefaultPreference.setName(groupName);
}, [groupName]);
const getPreference = async (key: string) => {
return await DefaultPreference.get(key);
};
const setPreference = async (key: string, value: string | number | boolean) => {
await DefaultPreference.set(key, value);
};
const clearPreference = async (key: string) => {
await DefaultPreference.clear(key);
};
const getMultiplePreferences = async (keys: string[]) => {
return await DefaultPreference.getMultiple(keys);
};
const setMultiplePreferences = async (data: { [key: string]: string | number | boolean }) => {
await DefaultPreference.setMultiple(data);
};
const clearMultiplePreferences = async (keys: string[]) => {
await DefaultPreference.clearMultiple(keys);
};
const getAllPreferences = async () => {
return await DefaultPreference.getAll();
};
const clearAllPreferences = async () => {
await DefaultPreference.clearAll();
};
return {
getPreference,
setPreference,
clearPreference,
getMultiplePreferences,
setMultiplePreferences,
clearMultiplePreferences,
getAllPreferences,
clearAllPreferences,
};
};
export default useDefaultPreference;