react-native-async-storage / async-storage

An asynchronous, persistent, key-value storage system for React Native.
https://react-native-async-storage.github.io/async-storage/
MIT License
4.7k stars 466 forks source link

not working react native web #1096

Open adsalihac opened 4 months ago

adsalihac commented 4 months ago

What happened?

Getting Error

Version

1.23.1

What platforms are you seeing this issue on?

System Information

Error storing data ReferenceError: window is not defined
    at getValue (/node_modules/@react-native-async-storage/async-storage/lib/commonjs/AsyncStorage.js:69:52)

Steps to Reproduce

t1gu1 commented 4 months ago

Got the exact same issue. Using the latest expo release and start the dev for the web.

TheForgetTime commented 3 months ago

Got the exact same issue.

Using: "expo": "^51.0.9" "@react-native-async-storage/async-storage": "1.23.1" "redux-persist": "^6.0.0"

t1gu1 commented 3 months ago

Here's a workaround if you are using something like Zustand:

// Zustand Store
{
      ...,
      storage: createJSONStorage(() =>
        Platform.OS === "web" ? localStorage : AsyncStorage
      ),
      ...,
}
adsalihac commented 3 months ago

localStorage ?

coren-frankel commented 3 months ago

For those using supabase and seeing this in the stack trace too, check out https://github.com/supabase/supabase-js/issues/786

t1gu1 commented 3 months ago

localStorage ?

@adsalihac Yep, in web localStorage it's simply the native solution. (No even need to import anything to use it)

At least, Zustand is able to deal with that seamlessly.

adsalihac commented 3 months ago

i am using redux with presist , it is support ? @t1gu1 , also what about Zustand , it is better than redux ?

t1gu1 commented 3 months ago

@adsalihac Nope Redux is more complexe on that side. That being said, for you question I would 100% recommend Zustand if you just started your project!

We migrate all our project to use Zustand cause it's stupid easy to use and works really well! It makes look Redux over complicated for nothings.

adsalihac commented 3 months ago

what about state migration in zustand (like version update) , which zustand is using , official module right ?

it is possible to get data like this (store.getState()?.auth?.user) ?

t1gu1 commented 3 months ago

@adsalihac Yes and yes. Just try it. ;)

adsalihac commented 3 months ago

Thank you @t1gu1

samkk commented 3 months ago

I think I encountered the same problem. can I help me,please

Using: "expo": "~51.0.8", "@react-native-async-storage/async-storage": "1.23.1", "zustand": "^4.5.2",

ios error message [ReferenceError: Can't find variable: localStorage]

t1gu1 commented 3 months ago

Upgrade Expo package: (Just to be sure)

Where you create your Zustand store, simply add that for the storage: https://github.com/react-native-async-storage/async-storage/issues/1096#issuecomment-2148048977

Use AsyncStorage for mobile and localStorage for the web.

azakharchenko-msol commented 2 months ago

Confirmed the same issue use this code as workaround


    if (Platform.OS === 'web') {
      while (typeof window === 'undefined') {
        await new Promise(resolve => setTimeout(resolve, 100));
      }
    }
t1gu1 commented 1 month ago

For me it seems like this issue can be close. I works fine now using only AsyncStorage. Feels like it was a Expo issue.

sydro commented 1 week ago

some issue here with 2.0.0 version