Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda