在手机中反应本机异步存储更新详细信息



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

首先用于在异步存储中进行设置你写了-->

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

而不是这样写——>

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

以及从异步存储获取写下这一行-->

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();

最新更新