Closed JamesBrightman closed 1 year ago
I found a way to do this using FirestoreDataConverter
which I believe is the currently accepted method for adding type to return hook return data.
firestoreDataConverters.ts
import { FirestoreDataConverter, DocumentData, QueryDocumentSnapshot, SnapshotOptions, Timestamp } from "firebase/firestore";
export type entry = {
id?: string,
createdAt: Timestamp,
rating: number
}
export const entryConverter: FirestoreDataConverter<entry> = {
toFirestore: (entryData: entry): DocumentData => {
return entryData;
},
fromFirestore: (snapshot: QueryDocumentSnapshot, options: SnapshotOptions): entry => {
const {rating, createdAt} = snapshot.data(options);
return {rating, createdAt, id: snapshot.id};
},
};
Changes in original App.tsx
const [data, loading, error] = useCollectionData<entry>(
query(collection(db, "entries")).withConverter(entryConverter)
);
Now data
will be typed as entry
:)
I would like to add a type to the data returned by the useCollectionData firestore hook. Currently I am using it like so -
I would like the
data
const to be of typeEntryObject[]
. Currently I am declaring this at the start of the map function, which does work, but ideally I'd know the type ofdata
before mapping it. Is this currently possible?My firebase initialization file is -
Package versions
"firebase": "^9.15.0", "react-firebase-hooks": "^5.1.1",