Have getters ignore some state dependency change #1154
-
What problem is this solvingProvide a way to ignore some dependency changes in getter function. Use case, I use the normalized entities pattern where I have entities act as a mini frontend database and a list of ids to display the data I want: export const useBlogStore = defineStore('blog', {
state: (): BlogState => ({
ids: [],
entities: {},
status: 'initial',
}),
getters: {
userBlogs: (state: BlogState): Blog[] =>
Object.entries(state.entities)
.filter(([id]) => state.ids.includes(id))
.map(([, card]) => card),
isLoading: (state: BlogState): boolean => {
return state.status === 'loading';
}
},
actions: {
async loadUserBlogs() {
const blogs = await api.listBlogs()
for (const blog of blogs) {
this.ids.push(blog.id);
this.entities[blog.id] = blog;
}
}
loadOtherPeopleBlogs() {
// load additional data that changes the `entities` but not `ids`
}
}
}); In the above example, I use the
Proposed solutionMaybe provide a function similar to Describe alternatives you've consideredI looked at the Vue existing API, there is the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can indeed use Behind the scenes, a getter is just a computed property. |
Beta Was this translation helpful? Give feedback.
You can indeed use
toRaw()
to escape Vue reactivity tracking. Depending on the case, usingtoRaw(state)
,toRaw(this.$state)
, or eventoRaw(this).$state
should give you access to that.Behind the scenes, a getter is just a computed property.