React Native AsyncStorage storing values other than strings

前端 未结 6 1652
花落未央
花落未央 2021-01-03 17:46

Is there any way to store values other than strings with AsyncStorage? I want to store simple boolean values for example.

AsyncStorage.setItem(\'key\', \'ok\         


        
6条回答
  •  清歌不尽
    2021-01-03 18:02

    I suggest you use react-native-easy-app, through which you can access AsyncStorage synchronously, and can also store and retrieve objects, strings or boolean data.

    import { XStorage } from 'react-native-easy-app';
    import { AsyncStorage } from 'react-native';
    
    export const RNStorage = {// RNStorage : custom data store object
         token: undefined, // string type
         isShow: undefined, // bool type
         userInfo: undefined, // object type
     };   
    
    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); 
    

提交回复
热议问题