i have 2 useStates i want to append both when i call SubmitData
allValues
has many datas name ,age ,sex etc.. i want add domestic
also into allvalues so i am trying to use form data but i am getting a empty formData
const SubmitData = () => {
const formData = new FormData();
if (domestic != null) {
formData.append("domestic", domestic);
}
if (allValues != null) {
Object.entries(allValues).forEach(([key, value]) =>
formData.append(key, value)
);
}
const config = {
headers: {
Authorization: `token ` localStorage.getItem("sales"),
},
};
console.log(formData);
};
CodePudding user response:
You can simply do this way
if(domestic !== null) {
allValues.domestic = domestic
}
It will automatically populate your domestic
data to allValues
.
If you want to have formData
as a main variable
const formData = {
...allValues,
}
if(domestic !== null) {
formData.domestic = domestic
}
ES5 version
const formData = Object.assign({}, allValues);
if(domestic !== null) {
formData.domestic = domestic
}