My component is designed to accept an array of Villager
through its prop
from a parent component, which retrieves the array from this.$store.state
.
return this.$store.state.villages.find(value => value.id === 0).villagers
Afterwards, I use a this.$store.mutation
commit to modify the array. While the mutation does take effect (as shown in the screenshot below), the component does not re-render and continues to display 3 items instead of 2.
There seems to be a disconnect in the reactive chain somewhere, but I am unable to pinpoint the issue. I assumed that the props
value would trigger reactivity. Although the value has changed, the DOM does not reflect the update.
https://i.sstatic.net/fdg2o.png
Code Excerpts
I have extracted relevant portions for clarity:
store/index.ts
[...]
export default new Vuex.Store({
state: {
villages: [
{
id: 0,
name: 'Peniuli',
foundingDate: 20,
villagers: [
{
id: '33b07765-0ec6-4600-aeb1-43187c362c5a1',
name: 'Baltasar',
bloodline: 'Gent',
occupation: 'Farmer'
},
[...]
mutations: {
disbandVillager (state, payload) {
const villagerId = payload.id
const village = state.villages.find(value => value.id === 0)
console.debug('disbanding villager:', villagerId)
if (!_.isNil(village)) {
console.debug('before:', village.villagers)
_.remove(village.villagers, function (n) {
return n.id === villagerId
})
console.debug('after:', village.villagers)
}
}
},
[...]
Village.vue
<template>
<Villagers :villagers="playerVillage.villagers"></Villagers>
</template>
[...]
computed: {
playerVillage: function () {
return this.$store.state.villages.find(value => value.id === 0)
}
}
[...]
Villagers.vue
<template>
<v-container>
<v-card v-for="villager in villagers" :key="villager.id">
<v-row>
<v-col>
<v-card-title>Name: {{villager.name}}</v-card-title>
</v-col>
<v-col>
<v-card-title>Bloodline: {{villager.bloodline}}</v-card-title>
</v-col>
<v-col>
<v-card-title>Occupation: {{villager.occupation}}</v-card-title>
</v-col>
<v-col v-if="managable">
<DisbandButton :villager="villager"></DisbandButton>
</v-col>
</v-row>
</v-card>
</v-container>
</template>
<script>
import DisbandButton from '@/components/village/DisbandButton'
export default {
name: 'Villagers',
components: { DisbandButton },
props: [
'villagers',
'managable'
]
}
</script>
DisbandButton.vue
<template>
<v-btn @click="disbandVillager" color="red">Disband</v-btn>
</template>
<script>
export default {
name: 'DisbandButton',
props: ['villager'],
methods: {
disbandVillager: function () {
this.$store.commit('disbandVillager', { id: this.villager.id })
}
}
}
</script>