You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The loading slot is triggered when the $store == undefined , But as the writable store is of type T[] , it defaults to [] even when we explicitly pass undefined, so the loading state never gets triggered to over come this we can add type to writable to make it accept undefined value as its initial state, Here is the updated code
export function collectionStore<T>(
firestore: Firestore,
ref: string | Query | CollectionReference,
startWith: T[]|undefined = undefined
) {
let unsubscribe: () => void;
// Fallback for SSR
if (!firestore || !globalThis.window) {
console.warn('Firestore is not initialized or not in browser');
const { subscribe } = writable(startWith);
return {
subscribe,
ref: null,
}
}
const colRef = typeof ref === 'string' ? collection(firestore, ref) : ref;
const { subscribe } = writable<T[] | undefined>(startWith, (set) => {
unsubscribe = onSnapshot(colRef, (snapshot) => {
const data = snapshot.docs.map((s) => {
return { id: s.id, ref: s.ref, ...s.data() } as T;
});
set(data);
});
return () => unsubscribe();
});
return {
subscribe,
ref: colRef,
};
}
The text was updated successfully, but these errors were encountered:
The loading slot is triggered when the $store == undefined , But as the writable store is of type T[] , it defaults to [] even when we explicitly pass undefined, so the loading state never gets triggered to over come this we can add type to writable to make it accept undefined value as its initial state, Here is the updated code
The text was updated successfully, but these errors were encountered: