React learning journey just hit a new level of "Deep Understanding." Building a simple digital clock sounds easy, but the logic behind useEffect and useState has some tricky layers. I ran into three specific problems that taught me how React handles the DOM: 1. The "Ticking Bomb": At first, every time the state updated, a new setInterval was registered. The app was re-rendering and creating infinite intervals! I fixed this by wrapping the logic in useEffect. 2. The "Invisible Leak": Even after hiding the clock, the interval kept running in the background. I solved this by returning a cleanup functionclearInterval(a) to wipe the reference when the component unmounts or hides. 3. The "UI Lag": When I toggled the clock back on, it would show the old time for 1 second before the interval kicked in. I fixed this "stale UI" by adding an immediate setTime call right before starting the interval so the update is instant. It’s not just about making things work; it’s about making them efficient! Check it out here: Live Demo: https://lnkd.in/gsG7Jeaz Source Code: https://lnkd.in/gVBvdwWq #ReactJS #WebDev #JavaScript #CodingLogic #FrontendDevelopment
Great
Best of luck