Camera-Based Barcode Scanning in React
Food Tracker (React + Firebase)
Chapter 4 · Camera-Based Barcode Scanning in React
This chapter builds the piece of the app that feeds Chapter 3's Cloud Function a barcode in the first place — and it's the one chapter in this entire course with no Firebase involvement at all. Everything here runs in the browser, which is exactly why the component built in this chapter will reappear, unchanged, in Food Tracker (React + Express) when that sibling course reaches its own Chapter 4.
Requesting Camera Access
facingMode: "environment" requests the rear camera on a phone — the one actually useful for scanning a product. "user" would request the front-facing camera instead, which is the wrong default for this app entirely. getUserMedia also only works over HTTPS (or localhost during development) — a real deployment gotcha worth knowing now, though not one this course has to worry much about, since Chapter 12's Firebase Hosting serves everything over HTTPS by default.
Decoding Barcodes From Video Frames
Two genuinely different approaches exist. The native browser BarcodeDetector API is fast and built in — but as of general browser support, it's available in Chrome/Edge on Android and desktop, and not in Safari on iOS. A JS library like ZXing (@zxing/browser) works everywhere, at some added CPU cost, since it decodes frames in pure JavaScript rather than using a native implementation. The practical pattern: try BarcodeDetector where it exists, fall back to ZXing where it doesn't.
Wiring It to This Course's Own Backend
The hook above knows nothing about Firebase — it just calls onDetected(barcode). The parent component supplies that callback, and that's the only place this course's own identity shows up at all:
Food Tracker (React + Express)'s own ScanScreen will look identical except for one line — handleDetected will call its Express server's own POST endpoint instead of a Cloud Function. Everything above this line is shared, unmodified, code.
Where This Course Is Headed
Direct client writes to Firestore for the add-item flow (this scanned barcode's own destination), Security Rules as the real gatekeeper, expiry alerts, item history and search, marking items used, recipe lookup, Firebase Authentication, deployment, and a capstone.
Hands-On Exercises
Explain why this chapter's camera-scanning component will reappear unchanged in Food Tracker (React + Express). What is the one thing that actually differs between the two courses' use of it?
📄 View solutionExplain what happens if the useEffect cleanup function omits `stream?.getTracks().forEach(track => track.stop())`, and why this matters specifically for a scan screen a user might navigate to and away from repeatedly.
📄 View solutionExplain why leaving the ZXing fallback as an unimplemented stub could pass all of a developer's own testing and still be a real bug in production. Which users specifically would be affected, and why wouldn't Chrome-based testing ever catch it?
📄 View solutionChapter 4 Quick Reference
facingMode: "environment"— the rear camera, not the front-facing oneBarcodeDetector— native, fast, Chrome/Edge/Android; not on Safari/iOS — needs a ZXing fallback- Cleanup — always stop every track from the stream in the effect's cleanup function, or the camera stays on after unmount
- This component is backend-agnostic — it only calls one
onDetected(barcode)callback; the parent decides what happens next - This chapter's own throughline: the identical component will power Food Tracker (React + Express) too — only the parent's callback differs
- Next chapter: Building the Add-Item Flow with Direct Firestore Writes