How to handle complex objects and non-reactive in store #1925
Replies: 1 comment
-
Hello, I found a way to handle non-reactive objects : Store : // Project : fetch data in store
import { defineStore, acceptHMRUpdate } from 'pinia';
import { shallowRef, markRaw } from 'vue';
import { useAuthStore } from "@/stores/auth";
import ApiService from "@/core/services/ApiService";
const authStore = useAuthStore();
/**
* Customers store
*/
export const useCustomersStore = defineStore({
id: 'customers',
state: () => ({
customers: shallowRef([] as Array<ICustomer>), // Make it non-reactive > using shallowRef is best but it become reactive at some point, using markRaw here same
}),
getters: {
items: (state) => state.customers,
},
actions: {
async fetchCustomers(forceReload:boolean = false) {
if ( forceReload === true || this.customers.length === 0) {
// Make sure we get auth and an hotelId
await authStore.verifyHotelId()
.then((hotelId) => {
// HotelId as been received ( and auth is authenticated )
// Fetch data
ApiService
.mget([{resource: "/hotels", slug: hotelId}, {resource: "/customers"}])
.then((response) => {
console.log("Fetching::", response)
if (typeof response.data != "undefined")
this.customers = markRaw(response.data.map(d => markRaw(d))); // Make it non-reactive
})
.catch(({ response }) => {
console.error(response.data.errors);
});
})
.catch((error) => { console.error("fetchCustomers+verifyHotelId::ERROR", error) })
}
else console.info("fetchCustomers:: Already loaded", this.customers.length);
}
},
});
if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useCustomersStore, import.meta.hot))
} Component : <script lang="ts">
import { defineComponent, ref, toRefs, reactive, computed, onMounted, markRaw, isReactive } from "vue";
....
import { useCustomersStore } from "@/stores/_project"
import produce from "immer";
export default defineComponent({
name: "customers",
components: {
...
},
setup() {
const customersStore = useCustomersStore();
// Fetch data from store
async function fetchCustomersStore() {
await customersStore.fetchCustomers().then(() => {
// Produce a non-reactive object using immer
tableData.value = produce(customersStore.items, draft => {});
// SUCCESS !
});
}
onMounted(() => {
fetchCustomers(); // Execute fetch on mounted
});
...
return {
tableData,
};
},
});
</script>
I still need assistance on proper ways to mutate datas in store ? Do I have to user patch ?
Hope it's a proper method and hope it could help ! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I have to build a store with some different needs :
— Some objects will be complex and I need reactivity on it
How to replace entirely this object by a new one without losing reactivity ? Like new operation
How to replace / empty a specific key without losing reactivity ? Like modify operation
How to empty a specific key without losing reactivity ? Like blank operation
— Some object have to be non reactive
I need to fetch and store them and then use them but I don't want them to be reactive / modified ...
How to store a non reactive object in Pinia ?
Thanks a lot for your help 👍
Best regards,
Wilhem
Beta Was this translation helpful? Give feedback.
All reactions