How do I set multiple values within Asyncstorage

坚强是说给别人听的谎言 提交于 2020-02-27 07:39:09

问题


How do I set multiple values within Asyncstorge?.Because I'm trying to set token and user_id .These are the server response values. This is how response looks like

json

{ error: 0,
  data: 'User registered Successfully',
  userData:
   { pwd: 'lmlmlm',
     phone_no: '9898989',
     user_name: '',
     status: 1,
     date: 2018-10-24T07:12:20.656Z },
  user_id: 60,
  token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.' }

And in react native I've written like this after user registrtaion.

.then((response) => response.json())
            .then((responseData) =>
            {
                console.log(responseData);
                 AsyncStorage.setItem('id_token', responseData.token),
                 AsyncStorage.setItem('user_id', responseData.user_id),
                    Actions.firstScreen();

                    });

And in my home page I'm accessing the token and user_id within AsyncStorage as

 AsyncStorage.getItem('id_token').then((usertoken) =>{
                this.setState({'id_token' : usertoken});
                console.log(usertoken);
            })
            AsyncStorage.getItem('user_id').then((uid) => {
              this.setState({'user_id': uid});
              console.log(uid);
            })

But I'm getting only the token , consoled result of uid is getting as null.But when I consoled the responseData I've values within it.Please help me


回答1:


Please check this docs at link

So you can use like this:

const items = [['k1', 'val1'], ['k2', 'val2']]
AsyncStorage.multiSet(items, () => {
    //to do something
});

Cheer!




回答2:


var items = [['k1', 'val1'], ['k2', 'val2']]
AsyncStorage.setItem("KEY", JSON.stringify(items))


来源:https://stackoverflow.com/questions/52963308/how-do-i-set-multiple-values-within-asyncstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!