Vladislav
Vladislav

Reputation: 476

Get Firebase Doc ID

I'm trying to work with Firebase. For comfortable work I use "react-firebase-hooks".

How do I get the doc id and doc data at the same time to transfer them to props? So far I can only get the doc data.

const TodoList: FC = () => {
    const [user] = useAuthState(auth)
    const [todos] = useCollectionData(collection(db, `users/${user?.email}/todos`))

    return (
        <section className="todo-list">
            {todos?.map(todo => (
                <Todo isCompleted={todo.isCompleted} text={todo.text} />
            ))}
        </section>
    )
}

Upvotes: 1

Views: 371

Answers (1)

kboskin
kboskin

Reputation: 420

You should be able to achieve this using custom converter

const yourConverter = {
    toFirestore(post) {
        return { ... }
    },
    fromFirestore(snapshot, options) {
        const data = snapshot.data(options)
        return {
            id: snapshot.id,
            data: data.text,
        }
    },
}
const reference = firestore
.collection(`users/${user?.email}/todos`)
.withConverter(converter);

Related helpful documentation can be found here

Upvotes: 2

Related Questions