how to save username and password in cache ? react-native

前端 未结 2 1868
花落未央
花落未央 2021-01-26 23:16

I try to save my Username and Password in cache. I couldnt save two data so I try to save just username below code.

but when I enter letter to TextInput, data g

相关标签:
2条回答
  • 2021-01-26 23:59

    You could try to store json as string and later parse json during get

    savedata = (username,password) => {
     let value = {'username' : username, 'password':password}   
     await AsyncStorage.setItem('myKey', JSON.stringify(value));
        this.setState({myKey: value});
    
        console.log("deneme",value);
    } 
    
    constructor(){
        AsyncStorage.getItem("myKey").then((value) => {
             if (value){
             let valueParsed = JSON.parse(value)        
             this.setState({myKey: valueParsed});
             console.log("didmounth : ", valueParsed)
             }    
    })
    }
    
    0 讨论(0)
  • 2021-01-26 23:59

    AsyncStorage.setItem and this.setState are async funtion, so your console.log() will not be work as you wish. To console you can try

    savedata = async (value) => {
      try {
        await AsyncStorage.setItem("myKey", value);
        this.setState({myKey: value}, () => {
          console.log("deneme",value);
        })
      } catch (error) {
        console.log('Store error');
      }
    }
    
    0 讨论(0)
提交回复
热议问题