0
0
React Nativemobile~20 mins

Firestore CRUD operations in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firestore Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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');
ANothing happens because deleteDoc only marks the document for deletion but does not remove it.
BThe user document is marked as deleted but still visible in the UI until app restart.
CThe document is copied to a backup collection before deletion, so UI shows both original and backup.
DThe user document with ID 'abc123' is removed from Firestore and the UI updates to no longer show that user.
Attempts:
2 left
💡 Hint
Deleting a document removes it immediately from Firestore and listeners update the UI.
📝 Syntax
intermediate
2: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?
A
import { collection, setDoc } from 'firebase/firestore';
const tasksRef = collection(db, 'tasks');
await setDoc(tasksRef, { title: 'Buy milk' });
B
import { collection, addDoc } from 'firebase/firestore';
const tasksRef = collection(db, 'tasks');
await addDoc(tasksRef, { title: 'Buy milk' });
C
import { doc, setDoc } from 'firebase/firestore';
const tasksRef = doc(db, 'tasks');
await setDoc(tasksRef, { title: 'Buy milk' });
D
import { addDoc } from 'firebase/firestore';
await addDoc('tasks', { title: 'Buy milk' });
Attempts:
2 left
💡 Hint
Use addDoc with a collection reference to add with auto ID.
lifecycle
advanced
2: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;
}
AThe listener is removed when the component unmounts, preventing memory leaks.
BThe listener continues running even after unmount, causing duplicate updates.
CThe listener throws an error on unmount because unsubscribe is not a function.
DThe listener pauses but does not unsubscribe, so it resumes on remount.
Attempts:
2 left
💡 Hint
Returning unsubscribe from useEffect cleans up the listener.
🔧 Debug
advanced
2: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');
AupdateDoc fails because the document with 'nonexistentId' does not exist in Firestore.
BupdateDoc fails because the 'age' field is missing in the document.
CupdateDoc fails because doc() requires a fourth argument specifying the collection.
DupdateDoc fails because updateDoc only works with setDoc, not doc references.
Attempts:
2 left
💡 Hint
updateDoc requires the document to exist before updating.
🧠 Conceptual
expert
2: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?
AWrites are queued on the server and only appear in the UI after syncing.
BWrites fail immediately and show an error in the UI until connection is restored.
CWrites are saved locally and immediately reflected in the UI; they sync to Firestore when online.
DWrites are lost if offline; the app must retry manually when online.
Attempts:
2 left
💡 Hint
Firestore supports offline persistence that updates UI instantly.