AsyncStorage应该传递什么值?

时间:2018-02-05 00:27:52

标签: php mysql react-native

我目前是本地反应的初学者,我想知道我是否想使用AsyncStorage登录和存储用户数据,应该传递什么密钥和值? AsyncStorage.setItem('key', 'value');

     UserLogin = () =>{

    const { username }  = this.state ;


    fetch('https://www.example.com/React/user-login.php', {
     method: 'POST',
     headers: {
       'Accept': 'application/json',
       'Content-Type': 'application/json',
     },
     body: JSON.stringify({

       username: username,

     })

    }).then((response) => response.json())
         .then((responseJson) => {

           // If server response message same as Data Matched
          if(responseJson === 'Data Matched')
           {
             login(username, password).then(authenticationToken => {
  AsyncStorage.setItem('token', authenticationToken)
})

               //Then open Profile activity and send user email to profile activity.
               this.props.navigation.navigate('ProfileScreen', { username:username });

           }
           else{

             Alert.alert(responseJson);
           }

         }).catch((error) => {
           console.error(error);
         });

     }

如果我要说如果已登录,那么AsyncStorage.setItem('key', 'value');,那么如果我想存储说出用户的用户名以便我可以在他们的个人资料页面上显示它,那么这可能有用吗?

1 个答案:

答案 0 :(得分:0)

AsyncStorage参数都是字符串,因此您可以存储序列化的json数据或仅存储普通字符串。

// Some code that logs someone in and gets an authentication token which then is stored
login(username, password).then(authenticationToken => {
  AsyncStorage.setItem('token', authenticationToken)
})

// then somewhere else in your code
AsyncStorage.getItem('token').then(authenticationToken => {
  console.log(‘the token’, authenticationToken)
})