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: After refresh persistent store not return data #417

Open
thisroot opened this issue Dec 24, 2024 · 1 comment
Open

Bug: After refresh persistent store not return data #417

thisroot opened this issue Dec 24, 2024 · 1 comment

Comments

@thisroot
Copy link

Same situation with ObservablePersistMMKV

  1. Select type of storage
export const persistentStorage = Platform.OS === 'web' ? ObservablePersistLocalStorage  : ObservablePersistMMKV
  1. Set up store
export const auth$ = observable<Index>({
      isAuthorized: (): boolean => !!auth$.credentials.get()?.accessToken,
      loading: false,
      credentials: undefined,
      logOut: () => {
        auth$.credentials.delete()
        router.replace("/");
      },
      authRequest: async (data: UserData): Promise<boolean> => {
       ....
          if (response.status === 200) {
            auth$.credentials.set(response.data.data);
          }
       ...
  1. Set up persistence
syncObservable(auth$, {
  persist: {
    plugin: persistentStorage,
  }
})
  1. Check credentials
if (!auth$.credentials.get()) { // true - credentials not found
    return <Redirect href="/sign-in"/>
 }
 

After refreshing App credentials did not restoring

@jmeistrich
Copy link
Contributor

jmeistrich commented Dec 29, 2024

I think you're missing setting a name on the persist options: https://www.legendapp.com/open-source/state/v3/sync/persist-sync/#mmkv-rn. Multiple observables can use the same persistence plugin but need to save to a difference key inside of the persistence.

So this should fix it:

syncObservable(auth$, {
  persist: {
    plugin: persistentStorage,
    name: 'auth'
  }
})

Or did you just miss including that in the example and it's something else?

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