Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to append 2 usestates in react js

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);
    
  };

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading