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

203
Vistas
How to use Vuex to store modal component state in child component list?

I'm trying to use vuex to store a value that shows/hides a modal in a child component. The child component is rendered multiple times by its parent in a list. However, the modal is always showing the very last item in the list and I can see that my computed modal value from my vuex state is running the same number of times as the number of elements in the list.

Vuex state

export default function () {
  return {
    currentProduct: {},
    allProducts: [],
    editProductDialog: false
  };
}

Vuex mutations

export const setEditProductDialog = (state, editProductDialog) => {
  state.editProductDialog = editProductDialog
}

Parent component:

<div>
  <product
    v-for="product in allProducts"
    :key="product.id"
    :product="product"
  />
</div>

Child component:

<template>
<div class="row justify-start">
  <div class="col-6">
    <q-item clickable v-ripple @click="runSetEditProductDialog">
      <q-item-section avatar>
        <q-icon name="chevron_right" />
      </q-item-section>
      <q-item-section class="text-body1">{{ name }}</q-item-section>
    </q-item>
  </div>
  <q-dialog v-model="editDialog" no-backdrop-dismiss>
     ...
     ...
  </q-dialog>
</template>
...
<script>
export default {
  name: "Product",
  props: ["product"],
  data: () => ({
  }),
  methods: {
    ...mapMutations({
      setEditProductDialog: "product/setEditProductDialog"
    }),
    runSetEditProductDialog() {
      this.setEditProductDialog(true)
    },
  },
  computed: {
    ...mapState("product", ["editProductDialog"]),
    editDialog: {
      get() {
        console.log("in get")
        return this.editProductDialog;
      },
       set(value) {
        console.log("in set")
        this.setEditProductDialog(value);
      },
    },
  },
};
</script>

Like I said, the dialog always shows the very last product component and I can see the print statements in my computed section running the same number of times as the number of elements in allProducts.

How do I properly use vuex to store modal state when rendering a list of components?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Instead of boolean true/false try to set id:

@click="runSetEditProductDialog(product.id)"

runSetEditProductDialog(id) {
  this.setEditProductDialog(id)
},

and in your computed property compare it:

return this.editProductDialog === this.product.id
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