Exercise 3: Why Camera Cleanup Is Genuinely Harder Here — Possible Solution ==================================================================== WHAT A REACT LIFECYCLE HOOK GUARANTEES ------------------------------ A React component's useEffect cleanup function is guaranteed to run whenever that component unmounts, regardless of WHY it unmounts - navigating to a different route, a parent component re-rendering and removing it, or any other reason the component stops being displayed. The framework itself calls that cleanup function automatically, on every unmount path, without the developer needing to separately code each possible way the component could go away. WHY THIS CHAPTER'S TEMPLATE APPROACH CAN'T MATCH THAT ------------------------------ This chapter's scan.js only stops the camera stream on the one specific path it explicitly codes: a successful barcode detection. There is no equivalent "this page is going away, run this cleanup" hook that fires automatically regardless of how the user actually leaves - clicking a Cancel link, pressing the browser's back button, or simply closing the tab would each need their own explicitly-coded handler to stop the stream, and any exit path the developer didn't think to handle leaves the camera running with no framework-level safety net catching it. WHY THIS WORKS AS AN ANSWER ------------------------------ It correctly explains that React's lifecycle hook guarantees cleanup runs on every unmount path automatically, and correctly identifies that a plain template script has no equivalent guarantee - only the specific exit paths a developer manually codes are actually covered, making the cleanup problem genuinely and structurally harder to fully solve in this approach.