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

82
Visualizações
spread two different objects with ternary

I have the following code and I want to manipulate sate under certain condition how to add the targeted state to the state object ? I tried tow options none has worked

const trueArr = { a: true, b: true, c:true };
const falseArr ={ a: false, b: false, c: false };
    
    setSate({
      ...iniciSate,
     [foo == 'test'  ? trueArr : falseArr]
    });
    
    
    setSate({
      ...iniciSate,
     {foo == 'test' ? ...trueArr : ...falseArr}
    });

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

This is a javascript question and not React.

To conditionally add this or that object's properties to another (using spread):

const state = {x:1}
const trueArr = { a: true, b: true, c:true }
const falseArr ={ a: false, b: false, c: false }

console.log(
    {...state, ...(true ? trueArr : falseArr)},

    // or "false"

    {...state, ...(false ? trueArr : falseArr)}
)

When you write ...(true ? trueArr : falseArr), then the surrounding parentheses evaluates it first, so the spread operator (...) "knows" which of the two options it applies to (the evaluated result)

about 4 years ago · Santiago Gelvez Relatório

0

You were very close:

setSate({
  ...iniciSate,
 (foo == 'test') ? ...trueArr : ...falseArr
});
about 4 years ago · Santiago Gelvez 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