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

[Bug/Missing Feature] object/map storage doesn't provide reviver function #277

Open
douugdev opened this issue Aug 17, 2022 · 1 comment

Comments

@douugdev
Copy link
Contributor

Description of the issue
Passing an object with date to useMMKVStorage will return the string representation of the date on subsequent renders:

// inside component
const [value, setValue] = useMMKVStorage<{date: Date}>(storage, { date: new Date() });

// first renders while value isn't retrieved from cold storage
console.log(typeof value); // object

// after restarting the app
console.log(typeof value); // string 

Example of parsing without reviver:

item[1] = map ? JSON.parse(map) : null;

Expected behavior
object/map storage should have a reviver function parameter which is then passed to JSON.parse

PS.: I can probably fix it, just creating this issue confirming this is not working as intended

@ammarahm-ed
Copy link
Owner

ammarahm-ed commented Aug 18, 2022

The useMMKVStorage hook works in isolation from the main storage library. Hence if we do use a reviver function it would be at initialization. Or another option is to pass it into the hook but use it to fix already parsed object. In your case it's the date. Something like this:

const [value, setValue] = useMMKVStorage<{date: Date}>(storage, { date: new Date() }, {
parse: (value) => {
value.date = new Date(value.date);
return value;
}
});

A PR would be great.

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 a pull request may close this issue.

2 participants