-
Notifications
You must be signed in to change notification settings - Fork 408
-
In my experience I always need to convert firebase.firestore.Timestamps to Dates so I can work with JS date libs, like date-fns. That means I end up with annoying/gross pollutions like:
// in a component const commentsRef = post.ref.collection("comments"); const { data: commentsData } = useFirestoreCollectionData<PostComment>( commentsRef, { idField: "id", } ); const comments = commentsData ? commentsData.map(shallowTSToDates) : [];
// utils export const firestoreTsToDate = ({ seconds, _seconds, nanoseconds, }: SerializedTs): Date => new Timestamp(seconds || _seconds || 0, nanoseconds).toDate(); // for every value in a map, if the value looks like a Firestore Timestamp, // convert it to a JS Date export const shallowTSToDates = <T extends Record<string, unknown>>( object?: T ): T => mapValues(object, (v) => isNumber((v as any)?.seconds) && isNumber((v as any)?.nanoseconds) ? firestoreTsToDate(v as SerializedTs) : v ) as T;
💡 Would it make sense for reactfire's hooks to have the option to do this automatically?
Maybe something like:
// in a component const commentsRef = post.ref.collection("comments"); const { data: commentsData } = useFirestoreCollectionData<PostComment>( commentsRef, { idField: "id", convertTimestamps: true, } );
(Sidenote: I really want to be able to specify those ReactFireOptions globally once, because I'd always use the exact same options everywhere).
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 6
Replies: 2 comments
-
Anyone have thoughts on this? I'd be happy to brainstorm, contrib something, etc. Seems like low hanging fruit with a potential big increase in developer experience.
Beta Was this translation helpful? Give feedback.
All reactions
-
100% would be nice to have a nice approach to do this
Beta Was this translation helpful? Give feedback.