简体   繁体   中英

editing array items dynamically in react native

This is my state and array:

this.state = {
  arr: [{
    arrid: 0,
    arrEmail: '',
    arrName: '',
    arrPhone: ''
  }],
  id: 0,
  email: "",
  isChecked: true,
  name: "",
  phone: "",
  isOpen: false,
  editedEmail: '',
  editedName: '',
  editedPhone: ''
};

This is the place where they are getting edited:

updateEmail = value => {
  this.setState({ editedEmail: value });
};
updateName = value => {
  this.setState({ editedName: value });
};
updatePhone = value => {
  this.setState({ editedPhone: value });
};

This the function of editing an element dynamically:

editData = (row) => {
  this.refs.modal2.close();
  var arr3 = this.state.arr;
  var arrid = this.state.id;
  var index = arr3.indexOf(row);
  var editedName = this.state.editedName;
  var editedEmail = this.state.editedEmail;
  var editedPhone = this.state.editedPhone;

  this.setState({
    arrid: arrid,
    arrEmail: editedEmail,
    arrName: editedName,
    arrPhone: editedPhone
  });
}

But it is not getting edited. Firstly, they are not getting stored in.

arr3

Use this in your editData function:

this.setState(arr:[...arr3.map(item=>item.arrid===arrid?{...item,arrEmail:editedEmail, arrName:editedName, arrPhone:editedPhone }:item)]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM