score:7

Accepted answer

i think that, the code is usefull for you

updateusersdata() {

  const { gender, phone, address, cityid, image, signature } = this.state;
  const fd = new formdata();
  fd.append('image', image, image.name);
  fd.append('gender', gender);
  fd.append('phone', phone);
  fd.append('address', address);
  fd.append('cityid', cityid);
  fd.append('signature', signature);
  fd.append('_method', 'patch');

  axios.post(
    `users/${this.props.id}`,
    fd,
    { headers: { 'content-type': 'application/x-www-form-urlencoded' } }
  );
}

https://github.com/axios/axios/issues/97

score:0

axios.patch() doesn't accept formdata.

however, it does accept a json.

you could then map formdata into a json. or you can work with json from the start...

let userdata = {};
fd.foreach(function(value, key){
    userdata[key] = value;
});

axios.patch(`users/${this.props.id}`, userdata);

i am pretty sure you don't need to define headers explicitly. axios seems to do that internally.

note:

patch method is for updating a part of a resource (just email and gender, etc...)

put method is for updating the resource as a whole.

edit: cleaner way to do this

cleaner way to do this is just grabbing the whole form from a ref. for that you will need to import useref from react and hook it with your form (for example ref='formref') and then you don't need to append anything, you can just grab form data like so:

let fd = new formdata(formref.current); // grabs form data as is
let userdata = {}; // creates a user json
fd.foreach(function(value, key){
    userdata[key] = value;  // populates user json with form data
});

axios.patch(`users/${this.props.id}`, userdata);  // send user json to patch this resource

Related Query

More Query from same tag