Skip to content

Getter values are overwritten when return value is an array and is passed to ref #1162

Discussion options

You must be logged in to vote

This is expected in terms of JS, you are copying the references to the same objects (e.g. https://www.freecodecamp.org/news/primitive-vs-reference-data-types-in-javascript/). If you want to create a copy, you need to copy the array and objects:

const store = useCatDogStore();

const dogs = ref(store.catsAsDogs.map(c => ({...c})));

Also note that using destructuring for getters and state won't always work, so it's prefered to use storeToRefs() (https://pinia.vuejs.org/core-concepts/#using-the-store)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@BillyBobFry
Comment options

Answer selected by BillyBobFry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1160 on March 21, 2022 21:57.