Questions about the new setup syntax with effectScope() #623
-
Hey, defineStore('id', () => {
const apolloMeQuery = useQuery(ME_QUERY) // we are inside of an `effectScope()`
function someAction() {
const { result } = apolloMeQuery
}
return { someAction }
}) The new syntax finally allows us to place business logic inside of the stores which previously had to go into setup() (which is e.g. awesome in combination with Vue-Apollo and the Composition-API). But I'm wondering: Does it make an impact on performance What are pros and cons of both options? i guess tthe new syntax kind of blurs the line between getters and actions as there is no clear differentiation. Are there more differences? Would love to hear other opinions as I'm thinking about rewriting all stores with the new syntax to take addvantage of mentioned possibillity if needed. Thanks a ton for Pinia and all the hard work that went into this ❤️ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I'm glad you are enjoying Pinia! No syntax is recommended over the other. The setup syntax makes it sometimes much easier to write stores, especially when using other composition functions. The options api has slightly better devtools support because it's able to group state changes within actions. Note you can call any composable inside of defineStore('route', {
state() {
const route = useRoute()
// could even be return route
return {
path: useRoutePath()
}
}
}) |
Beta Was this translation helpful? Give feedback.
-
Oh that's neat and new to me! Thanks for mentioning this.
Could you please elaborate on why it's much easier? Is there an example hidden in the repo maybe I can have a look at? And since you make an example using the router: I'm wondering if it's possible (and makes sense from your point of view) to move navigation guards or some of it's logic into a store? Thanks for your answers, helps a lot. |
Beta Was this translation helpful? Give feedback.
I'm glad you are enjoying Pinia!
No syntax is recommended over the other. The setup syntax makes it sometimes much easier to write stores, especially when using other composition functions.
The options api has slightly better devtools support because it's able to group state changes within actions.
Note you can call any composable inside of
state()
since it's ran inside aneffectScope()
: