0

The input doesn't change it's text. It comes pre-filled from the database. For example, if it comes with the text: example, if I press for example the s key, it console logs examples but in the DOM in is still example. Here is the handle code:

handleChange = event => {
    this.setState({ [event.target.name]: event.target.value });
    console.log(event.target.name);
    console.log(event.target.value);
  };

and the input field:

<input 
  type="text" 
  className="form-control" 
  name="note" 
  id=" note" 
  value={this.state.insurance.note} 
  onChange={this.handleChange}
/>

EDIT (fixed the render problem, but I don't get the data that I want when I submit the form) Here is the code for my form:

handleSubmit = event => {
    event.preventDefault();
    var state = this.state;
    axios
      .put(`${API_URL}/` + state.id + `/update`, {
        tip: state.tip,
        date_exp: state.date_exp,
        date_notif: state.date_notif,
        note: state.note
      })
      .then(response => {
        console.log(response.data);
        // window.location = "/view";
      })
      .catch(error => {
        console.log(error);
      });
  }

and my button is a simple submit button:

<button className="btn btn-success" type="submit">Save</button>

2 Answers 2

2

imjared's answer is correct: your problem is in the handleChange function, where you wrongly update the state.
That function should be something like the following one:

handleChange = event => {
    const insurance = Object.assign({}, this.state.insurance);
    insurance[event.target.name] = event.target.value;

    this.setState({insurance});
  };

In the first line of the function, you create a deep copy of the current object insurance saved in the state. Then, you update the copied object, and finally update the state.

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

5 Comments

The thing is that now when I submit the form, it console.loges and empty object... Before it console.loged and object with the fileds that i have modified...
could you please post also the code of your submit button? :)
Well, if you mean that the console.log(response.data) returns an empty object, that depends on the fetch that is not working correctly. By looking at your request, I do wonder: isn't by, any chance, that you should have written state.insurance.date_exp, state.insurance.date_notif instead of state.date_exp... ? I mean, before you handled the change on the insurance object, but in that request it seems that you do not have any insurance object in the state
Yeah, you're right, so sorry for the dumb question... That was it... Thank you so much!!
Thanks, you too! ;)
2

You're changing this.state.note based on the name property of your input so it makes sense that this.state.insurance.note wouldn't see any updates.

If you try console.logging this.state above your <input>, I bet you'll see what you're after.

Comments

Your Answer

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