在手机中修复本机AsyncStorage更新详细信息

时间:2017-06-14 07:21:54

标签: react-native

我在开发Android应用程序时使用的是native native。我正在尝试使用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: ''
    })
  }

1 个答案:

答案 0 :(得分: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();