Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda