I have a vue3 frontend set up with pinia as store, like this:
export const useShopStore = defineStore(
"shop",
() => {
const state = reactive({
data: {
fruits: [],
owners: [],
},
});
let data_copy = {
fruits: [],
owners: [],
};
async function fetchData() {
const URL = `${API_URL}/shops`;
await axios
.get(URL)
.then((res) => {
state.data = { ...res.data };
data_copy = { ...res.data };
})
.catch((err) => {
if (err.response) {
console.log(err.response);
}
});
}
return { data, data_copy, fetchData };
},
{
persist: true,
}
);
PROBLEM: NO matter what i tried, the variable data_copy is always a reactive copy of data.state.
GOAL: What i want to achieve is that i fetch the data and assign it a.) to the reactive variable state.data as well as b.) to the NON-reactive variable data_copy
REASON: In the template, i have a checkbox group and a computed property to compare the arrays in state.data with the data object that was originally fetch, data_copy. If the state.data object changed, a button "Update changes" is enabled. Meaning, after fetching the data from the API and assigning the result to data_copy, data_copy should never change.
I also tried Object.assign({}, res.data)
instead of the deconstruction syntax, that also did not work.
QUESTION: How can i get a truly constant copy from the fetchData result independent of state.data?
Thanks for you help!