2
 "@react-native-async-storage/async-storage": "1.15.5",
 "react": "17.0.1",
 "react-native": "0.64.2",

Here's how I use my Async

fetch('',{
... //codes here
.then((response) => response.json())
      .then((responseJson) => {

     AsyncStorage.setItem('user_token', JSON.stringify(responseJson.token));
     AsyncStorage.setItem('user_id', JSON.stringify(responseJson.data));
     AsyncStorage.setItem('login_type', 'LocalLogin');
     
   });
});

then after that here's how I get my asyncstorage

const [getData, setGetData] = useState();

const initializeData = async() => {
    const userToken = await AsyncStorage.getItem('user_id');
    if(userToken)
    {
      setGetData(JSON.parse(userToken));
    }
  }

  useEffect(() => {
    initializeData();
  },[]);

useEffect(() => {
    getUserData();
,[navigation,isFocused]);

const getUserData = async() => {
 var formBody = JSON.stringify({email:`${getData.email}`});
 fetch('',{ ... //more codes here
}

I should get the updated data from my API but it seems like the getData is null and also it should work because I am re rendering the components. Fetch is working fine because I can login . the main problem i am having is the AsyncStorage . Did someone encounter this kind of problem?

Ginxxx
  • 1,602
  • 2
  • 25
  • 54

3 Answers3

0

You have a race condition where initializeData and getUserData will both be called on mount, and getData won't be set yet.

You can fix the race like this.

useEffect(() => {
    if (getData) getUserData();
,[navigation,isFocused,getData]);
windowsill
  • 3,599
  • 1
  • 9
  • 14
0

I saw it on my adb logcat that it can't store too much data

So what I did is I chopped my data and store only the needed data . That's what I did.

Ginxxx
  • 1,602
  • 2
  • 25
  • 54
0

AsyncStorage size is set to 6MB by default, but when it comes with a lot of data you have to increase the size.

If you want to store all data you can update the property seen below.

File: android/gradle.properties

AsyncStorage_db_size_in_MB=10

or

AsyncStorage_db_size_in_MB=15

depends on how much data you would like to store.

CuriousSuperhero
  • 6,531
  • 4
  • 27
  • 50