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

Storage data not updating to next scene #678

Closed
SitaMelark opened this issue May 13, 2024 · 2 comments
Closed

Storage data not updating to next scene #678

SitaMelark opened this issue May 13, 2024 · 2 comments

Comments

@SitaMelark
Copy link

SitaMelark commented May 13, 2024

"react-native": "^0.73.8"
"react-native-mmkv": "^2.12.2",
"@react-navigation/drawer": "^6.6.15",

Im having a trouble trying to update the info of the storage, when i try updating it and the i do a navigate to another screen, when printing the value inside the storage is getting an old data from a previous set to the storage.

This is inside the login:

useEffect(() => {
    if (cuentaSeleccionada !== undefined) {
      setToStorage("jwtToken", jwtToken);
      setToStorage("credentials", credentials);
      setToStorage("usuarioCuenta", JSON.stringify(cuentaSeleccionada));
      
      navigation.navigate("ControlHorario");
    }
  }, [cuentaSeleccionada]);

This is setToStorage class:

import { MMKV } from "react-native-mmkv";

const storage = new MMKV();

export const setToStorage = (key: string, value: string) => {
  storage.set(key, value);
};

export const getFromStorage = (key: string) => {
  const storedData = storage.getString(key);
  const initialData = storedData ? JSON.parse(storedData) : '';

  return initialData;
};

export const deleteAllFromStorage = () => {
  storage.clearAll();
};

Then i print at "ControlHorario" scene the getFromStorage("jwtToken") and im getting old values. Im using drawer for navigation, located at the App.tsx file (Its the main file). Not sure if this is enough explanation or need more info

@mrousavy
Copy link
Owner

This is probably related to timing / navigation stuff - I don't think this is a bug in react-native-mmkv. If it is, could you please create a reproduction without any other libraries, so just react-native-mmkv calls?

@SitaMelark
Copy link
Author

This is probably related to timing / navigation stuff - I don't think this is a bug in react-native-mmkv. If it is, could you please create a reproduction without any other libraries, so just react-native-mmkv calls?

So i actually found the issue, was my fault because i was calling an old instance instead of the new one.

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

No branches or pull requests

2 participants