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?
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