Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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

  const changeHandler = (e) => {
    setAllValues({ ...allValues, [e.target.name]: e.target.value });
  };

   const SubmitData = () => {
    console.log(domestic);
    
    const formData = { ...allValues };
    if (!domestic) {
      formData.domestic = domestic;
    }
    var custumer = parseInt(id);
    if (!custumer) {
      formData.customer = custumer;
    }

when allValues is empty and i only pass value of domestic i am getting the floowlowing error

TypeError: Cannot set properties of undefined (setting 'domestic')

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

UPDATED

Since you have been facing undefined problem, I update the logic a bit

const formData = {...allValues}
if (!domestic_voilence) {
   formData.domestic = domestic_voilence;
}
const customer = id ? parseInt(id) : null;
if (!customer) {
   formData.customer = customer;
}
//TODO: submit your form data instead

ORIGINAL

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 the main variable

const formData = {
   ...allValues,
}
if(domestic !== null) {
   formData.domestic = domestic
}

ES5 version

const formData = Object.assign({}, allValues);
if(domestic !== null) {
   formData.domestic = domestic
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!