Add `stateToRefs` helper
See original GitHub issueWhat problem is this solving
If a store exposes lots of getters and state properties then extracting them is a little bit messy and repetitive:
import { computed } from "@vue/composition-api";
import useStore from "./useStore";
setup () {
const store = useStore();
return {
isLoading: computed(() => store.isLoading),
count: computed(() => store.count),
items: computed(() => store.items),
user: computed(() => store.user),
settings: computed(() => store.settings),
// ...and so on
add: store.add,
update: store.update,
delete: store.delete,
};
}
I suggest adding a stateToRefs
helper that would only convert stat properties and getters to reactive refs. I’m not sure about the name of this helper, stateToRefs
isn’t probably the best because one might think it would only convert state properties, but at the moment I have no idea for a more generic name that would work for both: getters and state.
Proposed solution
import { stateToRefs } from "pinia";
import useStore from "./useStore";
setup () {
const store = useStore();
// stateToRefs would only convert getters and state properties
const { isLoading, count,items, user, settings } = stateToRefs(store)
return {
isLoading,
count,
items,
user,
settings,
add: store.add,
update: store.update,
delete: store.delete,
};
}
Describe alternatives you’ve considered
We can do:
import { toRefs } from "@vue/composition-api";
import useStore from "./useStore";
setup () {
const store = useStore();
const { isLoading, count, items, user, settings } = toRefs(store)
return {
isLoading,
count,
items,
user,
settings,
add: store.add,
update: store.update,
delete: store.delete,
};
}
But this isn’t optimal because it not only converts getters and state to refs, but also wraps in refs all built-in methods (like $reset
) and all actions.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Add `stateToRefs` helper · Issue #565 · vuejs/pinia - GitHub
I suggest adding a stateToRefs helper that would only convert stat properties and getters to reactive refs. I'm not sure about the name...
Read more >Introduction - Pinia
You will find more information about each map helper in the core concepts. ... No need to dynamically add stores, they are all...
Read more >How to pass reactive data to a composable in Vue 3
You'll need to convert your state to refs and pass a ref for reactivity to work properly: useSimpleCalculator(toRef(state, 'myObjectList'));.
Read more >PDF file created from a TIFF image by tiff2pdf
State To Refs to the People For Their Approval ... Assistant Attorney General. ... day of May, A. D. 1935, there was filed...
Read more >Complex Vue 3 state management made easy with Pinia
We also changed the Authors link from /about to /authors and removed all default styles and added our own for the navbar class,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the tip. I didn’t think of using this property. If getters were also exposed this way then we could do:
Another alternative to a
stateToRefs
helper would be adding$asRefs
property tostore
. e.g.:Where
store.$asRefs.getters
would be getters converted to refs andstore.$asRefs.state
would be state converted to refs.Or maybe even better:
Where
store.$asRefs
would be both: getters and state converted to refs.In order to avoid unnecessary overhead these properties could utilize Proxies that would wrap properties in refs on demand (when they are accessed rather than when they are created)
If it was up to me I would rather go for a standalone
stateToRefs
helper because it imitates the way how Vue 3toRefs
works, so it would be probably easier to understand and feel more natural for people who already know Composition API.What about a function
storeToRefs()
that transform all state and getters of a store to refs? This avoids spread operations and is tree shakable. It also extracts properties added by plugins:Naming it
stateToRefs()
would be misleading since it also transfer gettersFor setup stores, this would retrieve any reactive source (ref, reactive, customRef, computed).