React Native AsyncStorage fetches data after rendering

后端 未结 4 1839
一整个雨季
一整个雨季 2020-12-13 04:11

I am using AsyncStorage in ComponentWillMount to get locally stored accessToken, but it is returning the promise after render()<

4条回答
  •  生来不讨喜
    2020-12-13 04:43

    you can use react-native-easy-app that is easier to use than async storage. this library is great that uses async storage to save data asynchronously and uses memory to load and save data instantly synchronously, so we save data async to memory and use in app sync, so this is great.

    import { XStorage } from 'react-native-easy-app';
    import { AsyncStorage } from 'react-native';
    
    const initCallback = () => {
    
         // From now on, you can write or read the variables in RNStorage synchronously
    
         // equal to [console.log(await AsyncStorage.getItem('isShow'))]
         console.log(RNStorage.isShow); 
    
         // equal to [ await AsyncStorage.setItem('token',TOKEN1343DN23IDD3PJ2DBF3==') ]
         RNStorage.token = 'TOKEN1343DN23IDD3PJ2DBF3=='; 
    
         // equal to [ await AsyncStorage.setItem('userInfo',JSON.stringify({ name:'rufeng', age:30})) ]
         RNStorage.userInfo = {name: 'rufeng', age: 30}; 
    };
    
    XStorage.initStorage(RNStorage, AsyncStorage, initCallback); 
    

提交回复
热议问题