March 4, 2026 • Week 9
console.firebase.google.comnpm install firebasegetDatabase()ref(db, "path")"people/sarah")import { getDatabase, ref } from 'firebase/database';
const db = getDatabase();
const peopleRef = ref(db, "people");
const sarahRef = ref(db, "people/sarah");
set() — replaces the entire value at a referencepush() — auto-generates a unique key and adds a new child (for array-like structures).then() and .catch() for handlingimport { ref, set as firebaseSet } from 'firebase/database';
// set() replaces the entire value
firebaseSet(sarahRef, 43)
.then(() => console.log("saved!"))
.catch(err => console.log(err));
import { ref, push as firebasePush } from 'firebase/database';
// push() auto-generates a unique key
firebasePush(taskRef, { description: 'First things first' });
onValue() sets up a real-time listener that fires whenever data changessnapshot.val() to get the actual dataimport { ref, onValue } from 'firebase/database';
const unregisterFunction = onValue(amitRef, (snapshot) => {
const amitValue = snapshot.val();
});
useEffect can return a cleanup function that runs when the component unmountsuseEffect(() => {
const db = getDatabase();
const amitRef = ref(db, "people/amit");
const unregisterFunction = onValue(amitRef, (snapshot) => {
const data = snapshot.val();
// update state with data
});
// cleanup: disconnect listener on unmount
return () => unregisterFunction();
}, []);
push() instead of manual indexing for concurrent access safetyObject.keys()// Convert Firebase object to array
const allTasksKeys = Object.keys(allTasksObject);
const allTasksArray = allTasksKeys.map((key) => {
const task = { ...allTasksObject[key] };
task.key = key; // save key for later reference
return task;
});
git checkout starter
git pull
git checkout main
git merge starter --no-edit