2017-06-14 49 views
0

我在開發android應用程序時使用本機反應。我正嘗試使用AsyncStorage更新手機中的配置文件。輸入完所有內容並保存我的配置文件後,它不會改變任何內容。名字仍然是一樣的。幫助反應手機中的本機AsyncStorage更新詳情

xport default class ControlPanel extends Component { 
    constructor(){ 
    super(); 
    this.state = { 
     modalVisible: false, 
     firstName: '', 
     middleName: '', 
     lastName: '', 
     address: '', 
     contactNumber: '' 
    } 
    this.editUser = this.editUser.bind(this); 
    } 

    componentDidMount() { 
    AsyncStorage.getItem("id").then(val => { 
     this.setState({ 
      "id": val 
     }) 
    }).done() 
    } 

    editUser(){ 
    fetch('http://plakaco.000webhostapp.com/api/createClient', { 
     method: 'POST', 
     headers: { 
     'Accept': 'application/json', 
     'Content-Type': 'application/json' 
     }, 
     body: JSON.stringify({ 
     c_fname: this.state.firstName, 
     c_mname: this.state.middleName, 
     c_lname: this.state.lastName, 
     c_address: this.state.address, 
     c_type: 'mobile', 
     c_contact: this.state.contactNumber 
     }) 
    }).then(response => { 
     response.json().then(data => { 
     AsyncStorage.setItem("id", data.data.client_id.toString()) 
     this.setState({ 
      "id": data.data.client_id.toString() 
     }) 
     }) 
     alert('User saved successfully!') 
     this.clearState() 
    }).catch(err => { 
     alert('Save failed') 
     this.clearState() 
    }) 
    } 

    clearState(){ 
    this.setState({ 
     firstName: '', 
     middleName: '', 
     lastName: '', 
     address: '', 
     contactNumber: '' 
    }) 
    } 
+0

你在哪裏調用了editUser函數? –

回答

0

所有在AsyncStorage 設置首先,你必須寫 - >

AsyncStorage.setItem("id", data.data.client_id.toString()) 

的而不是這樣寫 - >

AsyncStorage.setItem("id", JSON.stringify(data.data.client_id)) 

,並從AsyncStorage獲得 填寫此行 - >

AsyncStorage.getItem("id").then((value) => { 
    console.log("Your data is Here::-",value); 
    // instead of console.log you can set this value into the state like this. 
    this.setState({ 
     yourStateName:value 
    }) 
}).done();