Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
How to get the ID of a document after adding it into firestore

I'm new to Firebase. Right now I'm building a Todo List app with React, my code is the following:

    ...
    const todoItem = useRef(null);
    const pathRef = collection(firestore, `users/${user.uid}/todos`)
    const [todos] = useCollectionData(pathRef);


    const handleSubmit = async e => {
        e.preventDefault();
        const text = todoItem.current.value
        if(text) {
            try {
                const docRef = await addDoc(pathRef, {
                    text,
                    completed: false,
                    createdAt: serverTimestamp(),
                });
                console.log("This doc's id is: ", docRef.id)
              } catch (e) {
                console.error("Error adding document: ", e);
              }
        }
    };
...

Below my List component I have a TodoItem component:

...
    const onDelete = (id) => deleteDoc(doc(pathRef, '/?????'))

    return (
        <div key={id} className="...">
            <li className="...">{text}</li>
            <button onClick={() => onDelete(id)} className="...">Delete</button>
        </div>
    )
...

For the Delete button to work I need the unique docRef.id associated to that document, but I'm having troubles finding a way to fetch it from Firestore or saving it in my state in such way that remains related to it's corresponding document. I'm probably overcomplicating this too much, so I turn to you lovely coders from stackoverflow. What can I do?

about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda