react-native-mmkv 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/MMKV/CHANGELOG.md +14 -0
  2. package/MMKV/Core/Core.xcodeproj/project.pbxproj +1 -1
  3. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +1 -1
  4. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +1 -1
  5. package/MMKV/Core/MMKV.cpp +70 -7
  6. package/MMKV/Core/MMKV.h +9 -7
  7. package/MMKV/Core/MMKVPredef.h +1 -1
  8. package/MMKV/README.md +19 -19
  9. package/README.md +6 -2
  10. package/android/src/main/cpp/MmkvHostObject.cpp +79 -56
  11. package/ios/JSIUtils.mm +24 -25
  12. package/ios/MmkvHostObject.mm +44 -17
  13. package/ios/MmkvModule.mm +4 -1
  14. package/lib/commonjs/MMKV.js +142 -0
  15. package/lib/commonjs/MMKV.js.map +1 -0
  16. package/lib/commonjs/createMMKV.js +66 -0
  17. package/lib/commonjs/createMMKV.js.map +1 -0
  18. package/lib/commonjs/createMMKV.web.js +70 -0
  19. package/lib/commonjs/createMMKV.web.js.map +1 -0
  20. package/lib/commonjs/hooks.js +192 -0
  21. package/lib/commonjs/hooks.js.map +1 -0
  22. package/lib/commonjs/index.js +32 -0
  23. package/lib/commonjs/index.js.map +1 -0
  24. package/lib/module/MMKV.js +131 -0
  25. package/lib/module/MMKV.js.map +1 -0
  26. package/lib/module/createMMKV.js +55 -0
  27. package/lib/module/createMMKV.js.map +1 -0
  28. package/lib/module/createMMKV.web.js +60 -0
  29. package/lib/module/createMMKV.web.js.map +1 -0
  30. package/lib/module/hooks.js +174 -0
  31. package/lib/module/hooks.js.map +1 -0
  32. package/lib/module/index.js +3 -0
  33. package/lib/module/index.js.map +1 -0
  34. package/lib/typescript/MMKV.d.ts +130 -0
  35. package/lib/typescript/createMMKV.d.ts +6 -0
  36. package/lib/typescript/createMMKV.web.d.ts +2 -0
  37. package/lib/typescript/hooks.d.ts +70 -0
  38. package/lib/typescript/index.d.ts +2 -0
  39. package/package.json +1 -1
  40. package/react-native-mmkv.podspec +1 -1
@@ -0,0 +1,174 @@
1
+ import { useRef, useState, useMemo, useCallback, useEffect } from 'react';
2
+ import { MMKV } from './MMKV';
3
+
4
+ function isConfigurationEqual(left, right) {
5
+ if (left == null || right == null) return left == null && right == null;
6
+ return left.encryptionKey === right.encryptionKey && left.id === right.id && left.path === right.path;
7
+ }
8
+
9
+ let defaultInstance = null;
10
+
11
+ function getDefaultInstance() {
12
+ if (defaultInstance == null) {
13
+ defaultInstance = new MMKV();
14
+ }
15
+
16
+ return defaultInstance;
17
+ }
18
+ /**
19
+ * Use the default, shared MMKV instance.
20
+ */
21
+
22
+
23
+ export function useMMKV(configuration) {
24
+ const instance = useRef();
25
+ const lastConfiguration = useRef();
26
+ if (configuration == null) return getDefaultInstance();
27
+
28
+ if (instance.current == null || !isConfigurationEqual(lastConfiguration.current, configuration)) {
29
+ lastConfiguration.current = configuration;
30
+ instance.current = new MMKV(configuration);
31
+ } // @ts-expect-error it's not null, I promise.
32
+
33
+
34
+ return instance;
35
+ }
36
+
37
+ function createMMKVHook(getter) {
38
+ return (key, instance) => {
39
+ const mmkv = instance !== null && instance !== void 0 ? instance : getDefaultInstance();
40
+ const [value, setValue] = useState(() => getter(mmkv, key));
41
+ const valueRef = useRef(value);
42
+ valueRef.current = value; // update value by user set
43
+
44
+ const set = useCallback(v => {
45
+ const newValue = typeof v === 'function' ? v(valueRef.current) : v;
46
+
47
+ switch (typeof newValue) {
48
+ case 'number':
49
+ case 'string':
50
+ case 'boolean':
51
+ mmkv.set(key, newValue);
52
+ break;
53
+
54
+ case 'undefined':
55
+ mmkv.delete(key);
56
+ break;
57
+
58
+ default:
59
+ throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);
60
+ }
61
+ }, [key, mmkv]); // update value if key changes
62
+
63
+ const keyRef = useRef(key);
64
+ useEffect(() => {
65
+ if (key !== keyRef.current) {
66
+ setValue(getter(mmkv, key));
67
+ keyRef.current = key;
68
+ }
69
+ }, [key, mmkv]); // update value if it changes somewhere else (second hook, same key)
70
+
71
+ useEffect(() => {
72
+ const listener = mmkv.addOnValueChangedListener(changedKey => {
73
+ if (changedKey === key) {
74
+ setValue(getter(mmkv, key));
75
+ }
76
+ });
77
+ return () => listener.remove();
78
+ }, [key, mmkv]);
79
+ return [value, set];
80
+ };
81
+ }
82
+ /**
83
+ * Use the string value of the given `key` from the given MMKV storage instance.
84
+ *
85
+ * If no instance is provided, a shared default instance will be used.
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * const [username, setUsername] = useMMKVString("user.name")
90
+ * ```
91
+ */
92
+
93
+
94
+ export const useMMKVString = createMMKVHook((instance, key) => instance.getString(key));
95
+ /**
96
+ * Use the number value of the given `key` from the given MMKV storage instance.
97
+ *
98
+ * If no instance is provided, a shared default instance will be used.
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * const [age, setAge] = useMMKVNumber("user.age")
103
+ * ```
104
+ */
105
+
106
+ export const useMMKVNumber = createMMKVHook((instance, key) => instance.getNumber(key));
107
+ /**
108
+ * Use the boolean value of the given `key` from the given MMKV storage instance.
109
+ *
110
+ * If no instance is provided, a shared default instance will be used.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
115
+ * ```
116
+ */
117
+
118
+ export const useMMKVBoolean = createMMKVHook((instance, key) => instance.getBoolean(key));
119
+ /**
120
+ * Use an object value of the given `key` from the given MMKV storage instance.
121
+ *
122
+ * If no instance is provided, a shared default instance will be used.
123
+ *
124
+ * The object will be serialized using `JSON`.
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * const [user, setUser] = useMMKVObject<User>("user")
129
+ * ```
130
+ */
131
+
132
+ export function useMMKVObject(key, instance) {
133
+ const [string, setString] = useMMKVString(key, instance);
134
+ const value = useMemo(() => {
135
+ if (string == null) return undefined;
136
+ return JSON.parse(string);
137
+ }, [string]);
138
+ const setValue = useCallback(v => {
139
+ if (v == null) {
140
+ // Clear the Value
141
+ setString(undefined);
142
+ } else {
143
+ // Store the Object as a serialized Value
144
+ setString(JSON.stringify(v));
145
+ }
146
+ }, [setString]);
147
+ return [value, setValue];
148
+ }
149
+ /**
150
+ * Listen for changes in the given MMKV storage instance.
151
+ * If no instance is passed, the default instance will be used.
152
+ * @param valueChangedListener The function to call whenever a value inside the storage instance changes
153
+ * @param instance The instance to listen to changes to (or the default instance)
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * useMMKVListener((key) => {
158
+ * console.log(`Value for "${key}" changed!`)
159
+ * })
160
+ * ```
161
+ */
162
+
163
+ export function useMMKVListener(valueChangedListener, instance) {
164
+ const ref = useRef(valueChangedListener);
165
+ ref.current = valueChangedListener;
166
+ const mmkv = instance !== null && instance !== void 0 ? instance : getDefaultInstance();
167
+ useEffect(() => {
168
+ const listener = mmkv.addOnValueChangedListener(changedKey => {
169
+ ref.current(changedKey);
170
+ });
171
+ return () => listener.remove();
172
+ }, [mmkv]);
173
+ }
174
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["hooks.ts"],"names":["useRef","useState","useMemo","useCallback","useEffect","MMKV","isConfigurationEqual","left","right","encryptionKey","id","path","defaultInstance","getDefaultInstance","useMMKV","configuration","instance","lastConfiguration","current","createMMKVHook","getter","key","mmkv","value","setValue","valueRef","set","v","newValue","delete","Error","keyRef","listener","addOnValueChangedListener","changedKey","remove","useMMKVString","getString","useMMKVNumber","getNumber","useMMKVBoolean","getBoolean","useMMKVObject","string","setString","undefined","JSON","parse","stringify","useMMKVListener","valueChangedListener","ref"],"mappings":"AAAA,SAASA,MAAT,EAAiBC,QAAjB,EAA2BC,OAA3B,EAAoCC,WAApC,EAAiDC,SAAjD,QAAkE,OAAlE;AACA,SAASC,IAAT,QAAwC,QA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,IAAT,EAAe;AACb;AACAiB,MAAAA,SAAS,CAACC,SAAD,CAAT;AACD,KAHD,MAGO;AACL;AACAD,MAAAA,SAAS,CAACE,IAAI,CAACE,SAAL,CAAerB,CAAf,CAAD,CAAT;AACD;AACF,GATyB,EAU1B,CAACiB,SAAD,CAV0B,CAA5B;AAaA,SAAO,CAACrB,KAAD,EAAQC,QAAR,CAAP;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASyB,eAAT,CACLC,oBADK,EAELlC,QAFK,EAGC;AACN,QAAMmC,GAAG,GAAGnD,MAAM,CAACkD,oBAAD,CAAlB;AACAC,EAAAA,GAAG,CAACjC,OAAJ,GAAcgC,oBAAd;AAEA,QAAM5B,IAAI,GAAGN,QAAH,aAAGA,QAAH,cAAGA,QAAH,GAAeH,kBAAkB,EAA3C;AAEAT,EAAAA,SAAS,CAAC,MAAM;AACd,UAAM4B,QAAQ,GAAGV,IAAI,CAACW,yBAAL,CAAgCC,UAAD,IAAgB;AAC9DiB,MAAAA,GAAG,CAACjC,OAAJ,CAAYgB,UAAZ;AACD,KAFgB,CAAjB;AAGA,WAAO,MAAMF,QAAQ,CAACG,MAAT,EAAb;AACD,GALQ,EAKN,CAACb,IAAD,CALM,CAAT;AAMD","sourcesContent":["import { useRef, useState, useMemo, useCallback, useEffect } from 'react';\nimport { MMKV, MMKVConfiguration } from './MMKV';\n\nfunction isConfigurationEqual(\n left?: MMKVConfiguration,\n right?: MMKVConfiguration\n): boolean {\n if (left == null || right == null) return left == null && right == null;\n\n return (\n left.encryptionKey === right.encryptionKey &&\n left.id === right.id &&\n left.path === right.path\n );\n}\n\nlet defaultInstance: MMKV | null = null;\nfunction getDefaultInstance(): MMKV {\n if (defaultInstance == null) {\n defaultInstance = new MMKV();\n }\n return defaultInstance;\n}\n\n/**\n * Use the default, shared MMKV instance.\n */\nexport function useMMKV(): MMKV;\n/**\n * Use a custom MMKV instance with the given configuration.\n * @param configuration The configuration to initialize the MMKV instance with. Does not have to be memoized.\n */\nexport function useMMKV(configuration: MMKVConfiguration): MMKV;\nexport function useMMKV(configuration?: MMKVConfiguration): MMKV {\n const instance = useRef<MMKV>();\n const lastConfiguration = useRef<MMKVConfiguration>();\n\n if (configuration == null) return getDefaultInstance();\n\n if (\n instance.current == null ||\n !isConfigurationEqual(lastConfiguration.current, configuration)\n ) {\n lastConfiguration.current = configuration;\n instance.current = new MMKV(configuration);\n }\n\n // @ts-expect-error it's not null, I promise.\n return instance;\n}\n\nfunction createMMKVHook<\n T extends (boolean | number | string) | undefined,\n TSet extends T | undefined,\n TSetAction extends TSet | ((current: T) => TSet)\n>(getter: (instance: MMKV, key: string) => T) {\n return (\n key: string,\n instance?: MMKV\n ): [value: T, setValue: (value: TSetAction) => void] => {\n const mmkv = instance ?? getDefaultInstance();\n const [value, setValue] = useState(() => getter(mmkv, key));\n const valueRef = useRef<T>(value);\n valueRef.current = value;\n\n // update value by user set\n const set = useCallback(\n (v: TSetAction) => {\n const newValue = typeof v === 'function' ? v(valueRef.current) : v;\n switch (typeof newValue) {\n case 'number':\n case 'string':\n case 'boolean':\n mmkv.set(key, newValue);\n break;\n case 'undefined':\n mmkv.delete(key);\n break;\n default:\n throw new Error(`MMKV: Type ${typeof newValue} is not supported!`);\n }\n },\n [key, mmkv]\n );\n\n // update value if key changes\n const keyRef = useRef(key);\n useEffect(() => {\n if (key !== keyRef.current) {\n setValue(getter(mmkv, key));\n keyRef.current = key;\n }\n }, [key, mmkv]);\n\n // update value if it changes somewhere else (second hook, same key)\n useEffect(() => {\n const listener = mmkv.addOnValueChangedListener((changedKey) => {\n if (changedKey === key) {\n setValue(getter(mmkv, key));\n }\n });\n return () => listener.remove();\n }, [key, mmkv]);\n\n return [value, set];\n };\n}\n\n/**\n * Use the string value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [username, setUsername] = useMMKVString(\"user.name\")\n * ```\n */\nexport const useMMKVString = createMMKVHook((instance, key) =>\n instance.getString(key)\n);\n\n/**\n * Use the number value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [age, setAge] = useMMKVNumber(\"user.age\")\n * ```\n */\nexport const useMMKVNumber = createMMKVHook((instance, key) =>\n instance.getNumber(key)\n);\n/**\n * Use the boolean value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * @example\n * ```ts\n * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean(\"user.isPremium\")\n * ```\n */\nexport const useMMKVBoolean = createMMKVHook((instance, key) =>\n instance.getBoolean(key)\n);\n/**\n * Use an object value of the given `key` from the given MMKV storage instance.\n *\n * If no instance is provided, a shared default instance will be used.\n *\n * The object will be serialized using `JSON`.\n *\n * @example\n * ```ts\n * const [user, setUser] = useMMKVObject<User>(\"user\")\n * ```\n */\nexport function useMMKVObject<T>(\n key: string,\n instance?: MMKV\n): [value: T | undefined, setValue: (value: T | undefined) => void] {\n const [string, setString] = useMMKVString(key, instance);\n\n const value = useMemo(() => {\n if (string == null) return undefined;\n return JSON.parse(string) as T;\n }, [string]);\n const setValue = useCallback(\n (v: T | undefined) => {\n if (v == null) {\n // Clear the Value\n setString(undefined);\n } else {\n // Store the Object as a serialized Value\n setString(JSON.stringify(v));\n }\n },\n [setString]\n );\n\n return [value, setValue];\n}\n\n/**\n * Listen for changes in the given MMKV storage instance.\n * If no instance is passed, the default instance will be used.\n * @param valueChangedListener The function to call whenever a value inside the storage instance changes\n * @param instance The instance to listen to changes to (or the default instance)\n *\n * @example\n * ```ts\n * useMMKVListener((key) => {\n * console.log(`Value for \"${key}\" changed!`)\n * })\n * ```\n */\nexport function useMMKVListener(\n valueChangedListener: (key: string) => void,\n instance?: MMKV\n): void {\n const ref = useRef(valueChangedListener);\n ref.current = valueChangedListener;\n\n const mmkv = instance ?? getDefaultInstance();\n\n useEffect(() => {\n const listener = mmkv.addOnValueChangedListener((changedKey) => {\n ref.current(changedKey);\n });\n return () => listener.remove();\n }, [mmkv]);\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export * from './MMKV';
2
+ export * from './hooks';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAd;AACA,cAAc,SAAd","sourcesContent":["export * from './MMKV';\nexport * from './hooks';\n"]}
@@ -0,0 +1,130 @@
1
+ interface Listener {
2
+ remove: () => void;
3
+ }
4
+ /**
5
+ * Used for configuration of a single MMKV instance.
6
+ */
7
+ export interface MMKVConfiguration {
8
+ /**
9
+ * The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const userStorage = new MMKV({ id: `user-${userId}-storage` })
14
+ * const globalStorage = new MMKV({ id: 'global-app-storage' })
15
+ * ```
16
+ *
17
+ * @default 'mmkv.default'
18
+ */
19
+ id: string;
20
+ /**
21
+ * The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const temporaryStorage = new MMKV({ path: '/tmp/' })
26
+ * ```
27
+ */
28
+ path?: string;
29
+ /**
30
+ * The MMKV instance's encryption/decryption key. By default, MMKV stores all key-values in plain text on file, relying on iOS's sandbox to make sure the file is encrypted. Should you worry about information leaking, you can choose to encrypt MMKV.
31
+ *
32
+ * Encryption keys can have a maximum length of 16 bytes.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })
37
+ * ```
38
+ */
39
+ encryptionKey?: string;
40
+ }
41
+ /**
42
+ * Represents a single MMKV instance.
43
+ */
44
+ interface MMKVInterface {
45
+ /**
46
+ * Set a value for the given `key`.
47
+ */
48
+ set: (key: string, value: boolean | string | number) => void;
49
+ /**
50
+ * Get the boolean value for the given `key`, or `undefined` if it does not exist.
51
+ *
52
+ * @default undefined
53
+ */
54
+ getBoolean: (key: string) => boolean | undefined;
55
+ /**
56
+ * Get the string value for the given `key`, or `undefined` if it does not exist.
57
+ *
58
+ * @default undefined
59
+ */
60
+ getString: (key: string) => string | undefined;
61
+ /**
62
+ * Get the number value for the given `key`, or `undefined` if it does not exist.
63
+ *
64
+ * @default undefined
65
+ */
66
+ getNumber: (key: string) => number | undefined;
67
+ /**
68
+ * Checks whether the given `key` is being stored in this MMKV instance.
69
+ */
70
+ contains: (key: string) => boolean;
71
+ /**
72
+ * Delete the given `key`.
73
+ */
74
+ delete: (key: string) => void;
75
+ /**
76
+ * Get all keys.
77
+ *
78
+ * @default []
79
+ */
80
+ getAllKeys: () => string[];
81
+ /**
82
+ * Delete all keys.
83
+ */
84
+ clearAll: () => void;
85
+ /**
86
+ * Sets (or updates) the encryption-key to encrypt all data in this MMKV instance with.
87
+ *
88
+ * To remove encryption, pass `undefined` as a key.
89
+ *
90
+ * Encryption keys can have a maximum length of 16 bytes.
91
+ */
92
+ recrypt: (key: string | undefined) => void;
93
+ /**
94
+ * Adds a value changed listener. The Listener will be called whenever any value
95
+ * in this storage instance changes (set or delete).
96
+ *
97
+ * To unsubscribe from value changes, call `remove()` on the Listener.
98
+ */
99
+ addOnValueChangedListener: (onValueChanged: (key: string) => void) => Listener;
100
+ }
101
+ export declare type NativeMMKV = Pick<MMKVInterface, 'clearAll' | 'contains' | 'delete' | 'getAllKeys' | 'getBoolean' | 'getNumber' | 'getString' | 'set' | 'recrypt'>;
102
+ /**
103
+ * A single MMKV instance.
104
+ */
105
+ export declare class MMKV implements MMKVInterface {
106
+ private nativeInstance;
107
+ private functionCache;
108
+ private id;
109
+ /**
110
+ * Creates a new MMKV instance with the given Configuration.
111
+ * If no custom `id` is supplied, `'mmkv.default'` will be used.
112
+ */
113
+ constructor(configuration?: MMKVConfiguration);
114
+ private get onValueChangedListeners();
115
+ private getFunctionFromCache;
116
+ private onValuesAboutToChange;
117
+ set(key: string, value: boolean | string | number): void;
118
+ getBoolean(key: string): boolean | undefined;
119
+ getString(key: string): string | undefined;
120
+ getNumber(key: string): number | undefined;
121
+ contains(key: string): boolean;
122
+ delete(key: string): void;
123
+ getAllKeys(): string[];
124
+ clearAll(): void;
125
+ recrypt(key: string | undefined): void;
126
+ toString(): string;
127
+ toJSON(): object;
128
+ addOnValueChangedListener(onValueChanged: (key: string) => void): Listener;
129
+ }
130
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';
2
+ declare global {
3
+ function mmkvCreateNewInstance(configuration: MMKVConfiguration): NativeMMKV;
4
+ function nativeCallSyncHook(): unknown;
5
+ }
6
+ export declare const createMMKV: (config: MMKVConfiguration) => NativeMMKV;
@@ -0,0 +1,2 @@
1
+ import type { MMKVConfiguration, NativeMMKV } from 'react-native-mmkv';
2
+ export declare const createMMKV: (config: MMKVConfiguration) => NativeMMKV;
@@ -0,0 +1,70 @@
1
+ import { MMKV, MMKVConfiguration } from './MMKV';
2
+ /**
3
+ * Use the default, shared MMKV instance.
4
+ */
5
+ export declare function useMMKV(): MMKV;
6
+ /**
7
+ * Use a custom MMKV instance with the given configuration.
8
+ * @param configuration The configuration to initialize the MMKV instance with. Does not have to be memoized.
9
+ */
10
+ export declare function useMMKV(configuration: MMKVConfiguration): MMKV;
11
+ /**
12
+ * Use the string value of the given `key` from the given MMKV storage instance.
13
+ *
14
+ * If no instance is provided, a shared default instance will be used.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const [username, setUsername] = useMMKVString("user.name")
19
+ * ```
20
+ */
21
+ export declare const useMMKVString: (key: string, instance?: MMKV | undefined) => [value: string | undefined, setValue: (value: string | ((current: string | undefined) => string | undefined) | undefined) => void];
22
+ /**
23
+ * Use the number value of the given `key` from the given MMKV storage instance.
24
+ *
25
+ * If no instance is provided, a shared default instance will be used.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const [age, setAge] = useMMKVNumber("user.age")
30
+ * ```
31
+ */
32
+ export declare const useMMKVNumber: (key: string, instance?: MMKV | undefined) => [value: number | undefined, setValue: (value: number | ((current: number | undefined) => number | undefined) | undefined) => void];
33
+ /**
34
+ * Use the boolean value of the given `key` from the given MMKV storage instance.
35
+ *
36
+ * If no instance is provided, a shared default instance will be used.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const [isPremiumAccount, setIsPremiumAccount] = useMMKVBoolean("user.isPremium")
41
+ * ```
42
+ */
43
+ export declare const useMMKVBoolean: (key: string, instance?: MMKV | undefined) => [value: boolean | undefined, setValue: (value: boolean | ((current: boolean | undefined) => boolean | undefined) | undefined) => void];
44
+ /**
45
+ * Use an object value of the given `key` from the given MMKV storage instance.
46
+ *
47
+ * If no instance is provided, a shared default instance will be used.
48
+ *
49
+ * The object will be serialized using `JSON`.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * const [user, setUser] = useMMKVObject<User>("user")
54
+ * ```
55
+ */
56
+ export declare function useMMKVObject<T>(key: string, instance?: MMKV): [value: T | undefined, setValue: (value: T | undefined) => void];
57
+ /**
58
+ * Listen for changes in the given MMKV storage instance.
59
+ * If no instance is passed, the default instance will be used.
60
+ * @param valueChangedListener The function to call whenever a value inside the storage instance changes
61
+ * @param instance The instance to listen to changes to (or the default instance)
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * useMMKVListener((key) => {
66
+ * console.log(`Value for "${key}" changed!`)
67
+ * })
68
+ * ```
69
+ */
70
+ export declare function useMMKVListener(valueChangedListener: (key: string) => void, instance?: MMKV): void;
@@ -0,0 +1,2 @@
1
+ export * from './MMKV';
2
+ export * from './hooks';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-mmkv",
3
- "version": "2.1.1",
3
+ "version": "2.3.0",
4
4
  "description": "The fastest key/value storage for React Native. ~30x faster than AsyncStorage! Works on Android, iOS and Web.",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -26,6 +26,6 @@ Pod::Spec.new do |s|
26
26
  'ios/**/*.h'
27
27
  ]
28
28
 
29
- s.dependency "MMKV"
29
+ s.dependency "MMKV", ">= 1.2.13"
30
30
  s.dependency "React-Core"
31
31
  end