Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!