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

159
Visualizações
What's the best way to set boolean value in multiple reactive objects?

I have a large component, with lots of boolean reactive states and each DOM element has to change 4-5 of them whenever a button is clicked. MWE:

<button @click="state.val1 = false; state.val2 = false; state.val3 = false">click me</div>

<p>{{state.val1}}</p>
<p>{{state.val2}}</p>
<p>{{state.val3}}</p>

button is an example of such DOM elements and it gets confusing quickly so I was wondering if there is a better, cleaner way of doing that.

I've tried:

<button @click="state.val1, state.val2 = false">

and

<button @click="state['val1', 'val2'] = false">

both of which only change the second object val2.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Probably best to write a generic method:

const myObj = {
    state: {
        key1: false,
        key2: true,
        key3: false
    }
};

function changeState(name, keys, newVal) {
    Object.keys(this[name]).forEach(key => {
        if (keys.includes(key)) {
            this[name][key] = newVal;
        }
    })
}

changeState.call(myObj, 'state', ['key1', 'key2', 'key3'], false);

console.log(myObj);

changeState.call(myObj, 'state', ['key1', 'key3'], true);

console.log(myObj);

So in Vue, you'd get something like:

<button @click="changeState('state', ['key1', 'key2', 'key3'], false)">
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