CSFrequency / react-firebase-hooks

React Hooks for Firebase.
Apache License 2.0
3.6k stars 306 forks source link

useCollectionData return id as well #209

Closed taylorchance closed 2 years ago

taylorchance commented 2 years ago

It would be helpful to add an option that useCollectionData also returns the doc id as well.

qwyzex commented 2 years ago

Actually it's returning the document id in the previous version (I don't know which version start returning undefined id, but I use version 4.0.1 and it's returning the full document id!)

chrisbianca commented 2 years ago

As of react-firebase-hooks v5, the library relies on Firestore's built in FirestoreDataConverter functionality rather than the bespoke method used previously. This gives access to the underlying doc id and ref fields if required.

I've added an example to the docs here: https://github.com/CSFrequency/react-firebase-hooks/tree/master/firestore#transforming-data

Let me know if there are any issues

LGR3VE commented 2 years ago

@chrisbianca Thank you for the example in the docs. Do you have an example for JS, besides TS as well? I am struggling as well to include the Id by reading from the snapshot. Thank you so much!

chrisbianca commented 2 years ago

@LGR3VE the example for JS should be identical, you'll just need to drop the type annotations