-
-
Notifications
You must be signed in to change notification settings - Fork 103
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
useMMKVStorage value persistence #248
Comments
@dancixx What is the |
@ammarahm-ed yes, it is replaced, but If didn't set any defaultValue I got undefined. |
@dancixx I am unable to reproduce this issue. |
@ammarahm-ed I checked again soon. And try to investigate it. |
@dancixx Can you store and read values normally via setString/getString functions without hooks after app restart. Is default value persisted in storage? yes. Check by clearing app data then calling |
@ammarahm-ed when I used the getMap or getMapAsync calls I got method call error so it work with hook only but the persisted value is gone after first render. |
@dancixx Here's the simple example that is working: const storage = new MMKVStorage.Loader()
.withEncryption()
.withPersistedDefaultValues()
.initialize();
const useStorage = create(storage);
const App = () => {
const [user, setUser] = useStorage('user', 'robert'); // Default value is "robert"
console.log(storage.getString('user')); // on rerender value should always be "andrew"
if (user === "robert") { // Change default username on first launch to andrew
setUser("andrew");
} |
@ammarahm-ed I tried this, but the default value comes from the backend, so there is a moment where the value is different or undefined. I need a bit more time to check this again. |
@dancixx maybe you can wrap it in a useEffect and update the value in storage once the backend sends the correct value? |
@ammarahm-ed What do you think this normal behaviour or something is bad in my app, so something other causes this issue? |
@dancixx The default value should be there before your component renders. Default values should be used when you know what a possible default value would be before runtime. if you can share the exact code for how you are getting the value from backend etc, I might be able to help you out better. |
@ammarahm-ed I got the persisted value on the first render, but if I set the defaultValue prop, it will be overwritten. Other things, when I leave the defaultValue as empty I get a type error because it is a required arg and the persisted value change to undefined.
|
@dancixx Are you calling |
@dancixx I created a simple example to reproduce this: import {useEffect} from 'react';
import MMKVStorage, {create} from 'react-native-mmkv-storage';
const storage = new MMKVStorage.Loader()
.withEncryption()
.withPersistedDefaultValues()
.initialize();
const useStorage = create(storage);
const App = () => {
const [appIcon, setAppIcon] = useStorage('appIcon', 'ic_launcher');
console.log('result: ', appIcon);
useEffect(() => {
// set appIcon on first launch only
if (appIcon !== 'ic_launcher_custom') {
console.log('SETTING APP ICON');
setAppIcon('ic_launcher_custom');
}
}, [appIcon, setAppIcon]);
return null;
};
export default App; Does this example correctly demonstrate your use case? And I could not reproduce the issue with the above example. On app reload/restart value is persisted normally. I tested this on Android device. |
@ammarahm-ed I tested now on iOS and Android too and your example is working fine. I don't know what caused this issue before but I am sure that never called removeItem method. Maybe some expo or react-native settings or whatever. |
useMMKVStorage hook with default values set would store but not update value when passing falsy params to setNewValue such as an empty string or the number 0 Refs ammarahm-ed#248
I have a question or rather a complaint. Why cant we just use the hooks without setting a default value i.e it would be initialized with the last stored value |
@Benjamin3443 That was fixed in v0.7.5 |
Can you give me an example on how to use the hook without setting an initial default value, the default value should be what is stored with that key. Thanks |
1 similar comment
Can you give me an example on how to use the hook without setting an initial default value, the default value should be what is stored with that key. Thanks |
Hello, i also noticed i have this issue.
solution, for the time being is to use removeItem("user") and any other certain keys i want to remove. I think clearStore() should be looked into. |
Describe the bug
When I use the hook to store data on the next app restart I am able to log the prevState but it will be replaced with the defaultValue. I misunderstood the correct usage maybe, or is it a bug maybe?
Expected behaviour
Platform Information:
The text was updated successfully, but these errors were encountered: