我有一个带有按钮的登录屏幕,记住我,当我连接时,我想将此信息存储在如下所示的asynsctorage中:

    if (this.isFormValid()) {
      AccountService.login(login, password)
        .promise.then(body => {
          console.log('onLoginClick -> body', body)
          Keyboard.dismiss()
          AsyncStorage.setItem('login', login)
          if (this.state.isRememberMeSelected) {
            console.log("i'm in the storage !")

            AsyncStorage.multiSet([['login', login], ['password',
              password], ['isRememberMeSelected', '1']])
          } else {
            AsyncStorage.multiSet([['login', ''][('password', '')],
            ['isRememberMeSelected', '0']])
          }


当我回到抽屉时,我尝试读取存储并获取我的值“ isRememberMeSelected”:

 async componentDidMount() {
    // await AsyncStorage.getItem('login').then(value => {
    //   console.log('DrawerLogin', value)
    //   //this.setState({login: value})
    // })
    // await AsyncStorage.getItem('password').then(value => {
    //   console.log('DrawerPassword', value)
    //   //this.setState({password: value})
    // })
    await AsyncStorage.getItem('isRememberMeSelected').then(value => {
      console.log('DrawerIsRememberMeSelected', value)

      this.setState({isRememberMeSelected: value})
    })

    // await AsyncStorage.getAllKeys((err, keys) => {
    //   console.log(' Allkeys', keys)
    // })
    }


在我的抽屉中,我检查值“ isRememberMeSelected”以删除一些项目:

  StorageMultiRemove = async () => {
       try {
         await AsyncStorage.multiRemove(['refreshToken',
         'stripeCustomerId', 'token'], err => {
           console.log(' Dans multiRemove Error', err)
         })
       } catch (error) {
         // Error saving data
       }
     }



        isRememberMeSelected === '1' ? this.StorageMultiRemove() :
         AsyncStorage.clear()


当我回到抽屉中时,此值始终为null(除非我离开应用程序并重新启动),否则一切都设置妥当。

我非常适合我的状况(执行setItem之前的状况)...

我想将此数据记录在asynStorage中,返回到我的抽屉中并具有这些值

最佳答案

您可以添加一个将在获取异步存储时更改的变量

class Myform extends React.Component {
  state = {
    loading: true
}


然后在组件中进行mount更新状态

 componentDidMount() {
    await AsyncStorage.getItem('isRememberMeSelected').then(value => {
      console.log('DrawerIsRememberMeSelected', value)
      this.setState({
        isRememberMeSelected: value,
        loading: false
       })
    })
  }


并在渲染

render() {
   if(loading) return;  // could return loading spinner or something
}

关于javascript - AsyncStorage和Drawer渲染始终为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56442174/

10-09 23:13