First time using a store causes (wasteful?) re-render #1023
-
Hi! Using any store for the first time in a computed property means that property gets computed twice: once before the "Store installed" console message, and once after. I have no way to prevent the re-render, except by explicitly loading the store beforehand (which feels odd). Am I doing this right, or should I improve my code? Thanks for any help, and for a great library. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
if you are donig const store = useStore() instead |
Beta Was this translation helpful? Give feedback.
-
Ok, gotcha. I tried changing my code to match the documentation, and I tried mapStores(), but it's doing the same thing. computed: {
// name() {
// const character = useCharacterStore();
// console.log(character.name, "name compute");
// return character.name;
// }
...mapState(useCharacterStore, {
name(store) {
const name = store.name;
console.log("compute name");
return name;
}
})
} But it's the same thing:
Then I tried with computed: {
// name() {
// const character = useCharacterStore();
// console.log(character.name, "name compute");
// return character.name;
// }
...mapStores(useCharacterStore),
name() {
console.log("compute name");
return this.characterStore.name;
}
} I'm not loading any data or something, the store is just a very basic one... defineStore('character', {
state: () => ({
id: -1,
name: 'Fred',
characterImage: false,
})
}) Sincere apologies if I'm just misunderstanding something very basic here. |
Beta Was this translation helpful? Give feedback.
-
After looking at the Pinia source code, I think the issue comes fom Devtools being open. If Devtools is not open, the component is not rendered twice. See this plugin.ts code, which triggers an update inside a method explicitly called "addStoreToDevtools". Sorry for the confusion. Hopefully this info will benefit someone in the future, or lead to a small caveat in the docs about the side effects of having Devtools open. |
Beta Was this translation helpful? Give feedback.
After looking at the Pinia source code, I think the issue comes fom Devtools being open. If Devtools is not open, the component is not rendered twice. See this plugin.ts code, which triggers an update inside a method explicitly called "addStoreToDevtools".
Sorry for the confusion. Hopefully this info will benefit someone in the future, or lead to a small caveat in the docs about the side effects of having Devtools open.