What if your app could update data instantly without any hassle or mistakes?
Why Firestore CRUD operations in React Native? - Purpose & Use Cases
Imagine you have a notebook where you write down all your friends' contact details. Every time you want to add a new friend, update a phone number, or remove someone, you have to flip through pages manually and rewrite everything carefully.
This manual way is slow and easy to mess up. You might lose pages, write wrong numbers, or forget to update some details. It's hard to keep everything organized and up-to-date, especially when your list grows big.
Firestore CRUD operations let your app handle data like a smart assistant. You can add, read, update, and delete information quickly and safely without flipping pages. It keeps everything organized and instantly available to your app users.
const friends = [];
friends.push({name: 'Anna', phone: '123'});
// To update, find and change manuallyawait firestore.collection('friends').add({name: 'Anna', phone: '123'}); await firestore.collection('friends').doc(id).update({phone: '456'});
It makes your app dynamic and responsive, letting users see and change data instantly without confusion or delay.
Think of a chat app where messages appear live, or a shopping list app where you can add or remove items anytime, and everyone sees the changes immediately.
Manual data handling is slow and error-prone.
Firestore CRUD operations automate data management safely.
This makes apps faster, more reliable, and user-friendly.
Practice
collection() function do in Firestore when used in React Native?Solution
Step 1: Understand Firestore data structure
Firestore organizes data in collections which contain documents.Step 2: Role of
Thecollection()collection()function points to a specific collection (group of documents) in Firestore.Final Answer:
It points to a group of documents in the database. -> Option AQuick Check:
collection()= points to documents group [OK]
- Confusing collection() with doc()
- Thinking collection() creates data
- Mixing update and delete operations
Solution
Step 1: Identify correct Firestore add syntax in React Native
TheaddDocfunction adds a new document to a collection.Step 2: Check the syntax correctness
await addDoc(collection(db, 'users'), {name: 'Anna', age: 25}); usesaddDoc(collection(db, 'users'), data)which is the correct modern syntax.Final Answer:
await addDoc(collection(db, 'users'), {name: 'Anna', age: 25}); -> Option AQuick Check:
Add new doc = addDoc(collection(db, 'name'), data) [OK]
- Using setDoc without doc() reference
- Calling updateDoc on a collection instead of a document
- Using deprecated syntax from older SDK versions
const docRef = doc(db, 'tasks', 'task1');
await updateDoc(docRef, {completed: true});
const docSnap = await getDoc(docRef);
console.log(docSnap.data());Solution
Step 1: Understand updateDoc effect
TheupdateDoccall sets the fieldcompletedto true in the document 'task1'.Step 2: Reading updated document data
ThegetDocfetches the updated document, sodocSnap.data()returns the updated fields.Final Answer:
{ completed: true } -> Option BQuick Check:
updateDoc changes data, getDoc reads updated data [OK]
- Assuming updateDoc returns data
- Forgetting to await async calls
- Expecting old data without re-fetching
const docRef = doc(db, 'notes', 'note1'); await deleteDoc(collection(db, 'notes', 'note1'));
Solution
Step 1: Check deleteDoc argument type
ThedeleteDocfunction requires a document reference, not a collection reference.Step 2: Analyze the code's argument
The code passescollection(db, 'notes', 'note1')which is invalid because collection() does not accept a document ID.Final Answer:
deleteDoc should receive a document reference, not a collection. -> Option DQuick Check:
deleteDoc(docRef) not deleteDoc(collection(...)) [OK]
- Passing collection() instead of doc() to deleteDoc
- Misunderstanding collection() parameters
- Omitting await on async calls
Solution
Step 1: Check for field existence and condition
UsegetDocto read the document and verify if 'age' exists and is less than 30.Step 2: Conditionally update document
If condition is true, callupdateDocto update the 'status' field without overwriting other data.Final Answer:
Code snippet that reads, checks, then updates conditionally. -> Option CQuick Check:
Read first, then update if condition true [OK]
- Updating without checking field existence
- Using setDoc without merge causing overwrite
- Deleting document incorrectly after update
Ready to prove it?
Pass the test and this pattern moves unknown
