Challenge - 5 Problems
Firestore Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when this Firestore document is deleted?
Consider this React Native code snippet that deletes a Firestore document. What will be the result in the app UI after the deletion succeeds?
React Native
import { doc, deleteDoc } from 'firebase/firestore'; import { db } from './firebaseConfig'; async function deleteUserDoc(userId) { const docRef = doc(db, 'users', userId); await deleteDoc(docRef); console.log('User deleted'); } // Called on button press: deleteUserDoc('abc123');
Attempts:
2 left
💡 Hint
Deleting a document removes it immediately from Firestore and listeners update the UI.
✗ Incorrect
The deleteDoc function removes the document from Firestore immediately. If the UI listens to Firestore changes, it will update to reflect the deletion.
📝 Syntax
intermediate2:00remaining
Which option correctly adds a new Firestore document with auto-generated ID?
You want to add a new document to the 'tasks' collection with a field 'title' set to 'Buy milk'. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
Use addDoc with a collection reference to add with auto ID.
✗ Incorrect
addDoc requires a collection reference and data object. It creates a new document with an auto-generated ID.
❓ lifecycle
advanced2:00remaining
What is the effect of this Firestore listener in a React Native component?
This code sets up a Firestore listener inside a React Native component. What happens when the component unmounts?
React Native
import { useEffect, useState } from 'react'; import { collection, onSnapshot } from 'firebase/firestore'; import { db } from './firebaseConfig'; function TaskList() { const [tasks, setTasks] = useState([]); useEffect(() => { const unsubscribe = onSnapshot(collection(db, 'tasks'), (snapshot) => { setTasks(snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }))); }); return () => unsubscribe(); }, []); return null; }
Attempts:
2 left
💡 Hint
Returning unsubscribe from useEffect cleans up the listener.
✗ Incorrect
The unsubscribe function returned by onSnapshot is called on unmount to stop listening and free resources.
🔧 Debug
advanced2:00remaining
Why does this Firestore update code fail with a runtime error?
This code tries to update a Firestore document but throws an error. What is the cause?
React Native
import { doc, updateDoc } from 'firebase/firestore'; import { db } from './firebaseConfig'; async function updateUser(userId) { const userRef = doc(db, 'users', userId); await updateDoc(userRef, { age: 30 }); } updateUser('nonexistentId');
Attempts:
2 left
💡 Hint
updateDoc requires the document to exist before updating.
✗ Incorrect
updateDoc throws an error if the document does not exist. You must create it first or use setDoc with merge.
🧠 Conceptual
expert2:00remaining
How does Firestore handle offline writes in React Native apps?
When a React Native app writes data to Firestore while offline, what happens to the data and UI?
Attempts:
2 left
💡 Hint
Firestore supports offline persistence that updates UI instantly.
✗ Incorrect
Firestore caches writes locally and updates the UI immediately. When the device reconnects, it syncs changes to the server.