I have a state
object being passed through as a prop from another component. I receive the state
as expected. I then store the props
in a toRef()
for reactivity.
What I want to happen is when the data in state
changes, I want to data in the browser to update. As it is right now, the data does get updated in the vue dev tools and I do see the state
object change but the browser does not reflect the changes until I manually refresh the browser, which is an undesired result.
Why is this happening:
Component:
<template>
<div class="show-products">
<Product
v-for="data in products"
:key="data.id"
:product="data"
/>
</div>
</template>
<script>
import {computed, onMounted, toRefs, watch } from "vue";
import {useStore} from "vuex";
export default {
components: {Product},
props: {
state: Object
},
setup (props) {
const store = useStore();
const products = computed(() => store.getters.getProducts);
let stateRef = toRefs(props).state;
onMounted(() => {
store.dispatch('getProducts');
});
watch(stateRef, (currentState, prevState) => {
if (currentState.length !== prevState.length) {
store.dispatch('getProducts');
}
})
return {
Product,
products,
}
}
}
</script>