0

I need to edit array of objects and show new value to the view. I'm pretty new in ReactJS, so I tried do it like below, but after edit I lose everything except for the one I have edited. Can you give me info if my way to do this is correct? Any advices how to do this?

Parent component:

 constructor(props) {
  super(props);
  this.state = { data: [], open: false, openEdit: false, openAlert:false, priority:'', nameTask:'', deadline:new Date() };
}
  // Edit row from table
handleEdit = e => {    
 const index = e.currentTarget.getAttribute('index');
 let foundObject = this.state.data[index];

 let nameTaskNew = foundObject.nameTask;
 let priorityNew = foundObject.priority;
 let deadlineNew = foundObject.deadline;
 this.setState({openEdit: true, nameTask: nameTaskNew, priority: priorityNew, deadline: deadlineNew });
}

handleSubmitEdit = (e) => {
 const err = this.validate();
  if (!err) {
   this.setState({
     // Set new data array
     data: [this.state],
     // Clear form
     openEdit: false,
     nameTask: "",
     nameTaskError: "",
     priority: "Low",
     deadline: new Date()
   });}}

render() {
 const actions = [
 <FlatButton label="Edit" primary={true} keyboardFocused={true} onClick={e => this.handleSubmitEdit(e)} />];

return (
    {/* Edit form */}
    <form>
    <Dialog title="Edit your Task" open={this.state.openEdit} actions={actions}>
      <TextField floatingLabelText="Task" value={this.state.nameTask} errorText={this.state.nameTaskError}
      onChange={e => this.handleTextFieldChange(e)}  
      onKeyPress={this.handleKeyPress} />
      <DatePicker floatingLabelText="Deadline" value={this.state.deadline} onChange={this.handleChangeDate} />
      <SelectField floatingLabelText="Priority" value={this.state.priority} onChange={this.handleChangeSelectField}>
          <MenuItem value="High" primaryText="High" />
          <MenuItem value="Medium" primaryText="Medium" />
          <MenuItem value="Low" primaryText="Low" />
      </SelectField>
    </Dialog>  
  </form>
);}}

export default Home;
2
  • So to be clear you have an array of objects stored in this.state.data and you want to edit just one of those objects and keep the rest the same? Commented Jan 5, 2018 at 10:19
  • Exactly. I have feature to add new items to the table and I'm working on edit one of these item. Commented Jan 5, 2018 at 10:24

2 Answers 2

1

I suggest you check this post. I believe it's what you're looking for: https://medium.com/@thejasonfile/using-the-spread-operator-in-react-setstate-c8a14fc51be1

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are missing spreading the full state.

handleSubmitEdit = (e) => {
 const err = this.validate();
  if (!err) {
   this.setState({
     // Set new data array
     ...this.state,
     // Clear form
     openEdit: false,
     nameTask: "",
     nameTaskError: "",
     priority: "Low",
     deadline: new Date()
   });}}

This may help

1 Comment

Now, there is now error in console but item doesn't edit after all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.