question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

persist with sync storage

See original GitHub issue

Hi,

I’m using react-native-mmkv as a replacement to asyncstorage,

react-native-mmkv is a sync library, but when using it in the persist middlewar, it doesn’t rehydrate.

This is what I had to write to get it to work

export const storage: StateStorage = {
  getItem: async (key: string): Promise<string | null> => {
    const reply = MMKV.getString(key)
    return Promise.resolve(reply ?? null)
  },
  setItem: async (key: string, value: string): Promise<void> => {
    MMKV.set(key, value)
    Promise.resolve()
  },
}

but ideally, it should work synchronously

export const storage: StateStorage = {
    getItem: (key: string): string | null => {
      const reply = MMKV.getString(key)
      return reply ?? null
    },
    setItem: (key: string, value: string): void => {
      MMKV.set(key, value)
    },
  }
}

I’ve read a bit about the issue here and I was wondering it https://github.com/pmndrs/zustand/pull/455 fixes the issue, or maybe I should look elsewhere for a fix ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14

github_iconTop GitHub Comments

1reaction
austinriedcommented, May 12, 2022

Thanks for that link @AnatoleLucet, I’m not sure how I’d use hasHydrated in my case since what I want to do is call a function on the state once hydration has been finished, and that only seems to allow me to check if it’s been finished. I also tried to subscribe to onFinishHydration but wasn’t able to get that to work, I’m guessing because I was subscribing to it too late and hydration was already done.

But, I did read more into that and learned that sync storage is actually finished when the state is created, so in my case I just switched to calling the function on the state I wanted directly in my splash screen component instead of waiting for hydration and trying to call it in onRehydrateStorage/setting my own hydrated prop on the state, and now it’s working fine.

1reaction
jbrodriguezcommented, Jul 7, 2021

The error param is not set in onRehydrateStorage if that’s what you meant.

I’ll try a clean cache, etc. and retry setRehydrated, to check if it was something odd happening.

Read more comments on GitHub >

github_iconTop Results From Across the Web

console.error: "redux-persist failed to create sync storage ...
In redux-persist v6, you try changing as follows: Old config V5 => import storage from 'redux-persist/lib/storage'; const persistConfig ...
Read more >
redux-persist failed to create sync storage. falling back to noop ...
I have configured redux-perist in my React.js application like below. While launching application, I am getting below error in console.
Read more >
redux-persist failed to create sync storage. falling back to noop ...
Fixed code for nextjs:mightycoders.xyz/redux- persist -failed-to-create- sync - storage -falling-back-to-noop...
Read more >
storage.sync - Mozilla - MDN Web Docs
Represents the sync storage area. Items in sync storage are synced by the browser. The data is then available on all instances of...
Read more >
chrome.storage - Chrome Developers
The Storage API provides an extension-specific way to persist user data and state. ... Local and sync storage areas should not store confidential...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found