Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support set/get object #562

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ngocle2497
Copy link
Contributor

Add support set/get object, array, ...

@mrousavy
Copy link
Owner

Hey thanks for your PR!

I don't think this is a good thing to add to the library, I would prefer the JSON.stringify/JSON.parse stuff to stay in user code because this is making type assumptions that simply aren't true and aren't typechecked. I'll think about it.

@ngocle2497
Copy link
Contributor Author

@mrousavy , actually when use this library, I alway create a common function to save/get object each project. i think it will help someone like me

@fabianbru
Copy link

I would also love to have an get/set equivalent to the useMMKVObject method, which in fact also uses JSON.parse and JSON.stringify under the hood (

export function useMMKVObject<T>(
key: string,
instance?: MMKV
): [value: T | undefined, setValue: (value: T | undefined) => void] {
const [string, setString] = useMMKVString(key, instance);
const value = useMemo(() => {
if (string == null) return undefined;
return JSON.parse(string) as T;
}, [string]);
const setValue = useCallback(
(v: T | undefined) => {
if (v == null) {
// Clear the Value
setString(undefined);
} else {
// Store the Object as a serialized Value
setString(JSON.stringify(v));
}
},
[setString]
);
return [value, setValue];
}
).

Currently when I use the hook and also need some direct getter for an object/array, then I need to write a helper method. Types are not checked by my helper function either, but this is okay.

By the way, thank for this package, I migrated all projects from AsyncStorage to MMKV 👍 And I will use it with or without "get/setObject" 😉

@chj-damon
Copy link

@mrousavy well, recently I noticed that I don't have to JSON.stringify(object) when using set function to persist an object. I'm using 2.11.0, because when I use getString(key) and then JSON.parse it, I have to JSON.parse twice to get the original object I persisted. I read the souce code and find nothing.

is there anything I'm missing?

@chj-damon
Copy link

even the object I want to persist is an Object Array, I also don't need to JSON.stringify it. which confuses me since you said developers have to handle the parse/stringify themselves.

@chj-damon
Copy link

sorry about my comments, I'm using react-native-mmkv with atomWithStorage in jotai, setItem in createJSONStorage already stringify the value so I don't have to. which makes everything sense.

@mrousavy
Copy link
Owner

@mrousavy , actually when use this library, I alway create a common function to save/get object each project. i think it will help someone like me

Yea I know, but this is probably a good thing. If I add setObject and getObject functions, the user might assume that those objects are safely typed - which they are absolutely not. Your storage could get corrupted, someone else (notification extension? previous version of your app? etc) could write to that key, and your object might look entirely different from what the type suggests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants