I have some issues when trying to understand how reactivity is supposed to work, if we use the new store
concept.
E.g. I just have an array of products, like so:
products = [{
title: 'Product 1',
id: 1
}, {
title: 'Product 2',
id:2
}
]
When running the app, everything is fine at first (after finishing the request, the list shows up just fine), but then:
- I want a list to get updated, when a product changes (or the list ist sorted).
- I also want to ‘pin’ the first product to the top (e.g. after sorting, the ‘pinnedProduct’ is #2, else #1)
The problem is, that the list is only updated, if I use a computed property in the SFC
–
here’s what I have in the store (more or less taken from the examples available):
import {createStore, request} from 'framework7/lite';
const store = createStore({
state: {
products: [],
},
getters: {
// I want to keep a product reactively pinned to top
pinnedProduct({state}) {
return state.products[0];
},
products({state}) {
return state.products;
},
},
actions: {
getProducts({state}, url) {
request.get(url).then((res) => {
state.products = JSON.parse(res.data);
});
},
sortProducts({state}, direction = false) {
state.products = direction ? [...state.products.sort((a, b,) => a.id < b.id)] : [...state.products.sort((a, b,) => a.id > b.id)];
},
},
});
export default store;
in a Vue SFC:
<template>
<!-- accessing property: doesn't redraw when value changes -->
<f7-list v-if="pinned.value">
<f7-list-item>{{ pinned.value.title }}</f7-list-item>
</f7-list>
<!-- accessing store.getters directly: doesn't redraw when value changes -->
<f7-list v-if="store.getters.pinnedProduct.value">
<f7-list-item>{{ store.getters.pinnedProduct.value.title }}</f7-list-item>
</f7-list>
<!-- OK!! computed property redraws when value changes -->
<f7-list v-if="pinnedProduct.length">
<f7-list-item v-for="(value, key) in pinnedProduct"> {{ value.title}} </f7-list-item>
</f7-list> -->
</template>
<script>
import { onMounted } from 'vue';
import { useStore } from 'framework7-vue';
import store from '../js/store';
export default {
setup() {
const products = useStore('products');
onMounted(() => {
store.dispatch('getProducts');
});
return {
store,
products,
pinned: store.getters.pinnedProduct
};
},
computed: {
pinnedProduct() {
return store.getters.pinnedProduct.value;
}
},
};
</script>
How is reactivity supposed to work here?
Any insights?