Akhil Nayak’s Post

🚀 useEffect vs useEffectEvent 🚀 React’s Most Underrated Update Everyone knows useEffect. But only a few devs truly understand why React introduced useEffectEvent in React 19. And trust me this tiny hook changes how we handle side effects forever 👇 Before React 19, we’d often do this: useEffect(() => { const id = setInterval(() => { console.log(value); }, 1000); return () => clearInterval(id); }, [value]); Looks fine, right? But every time value changes, React clears and restarts the interval. 😬 Why? Because useEffect captures variables from its render (closure behavior). So if you omit dependencies, you get stale data. If you include them, you get unwanted re-runs. Classic React headache 😅 💡 The Fix — useEffectEvent React 19 introduces useEffectEvent to solve exactly this. It separates when your effect runs from what data it uses. Here’s how 👇 import { useEffect, useEffectEvent } from "react"; function Example({ value }) { const onTick = useEffectEvent(() => { console.log(value); // always fresh }); useEffect(() => { const id = setInterval(onTick, 1000); return () => clearInterval(id); }, []); // runs only once ✅ } Now the effect runs only once but still logs the latest value every time. No stale closures. No re-renders. No performance hit. ⚡ Think of it like this 👇 🕰️ useEffect → decides when to run ⚡ useEffectEvent → decides what happens inside Or simply put useEffect sets the timer, useEffectEvent updates the recipe inside it without restarting the timer 🍳 🎯 TL;DR ✅ useEffect — runs when dependencies change ✅ useEffectEvent — keeps logic fresh inside a stable effect ✅ Together → cleaner, faster, and bug-free React side effects So the next time you set up an interval, a subscription, or any long-running effect remember: you don’t need to re-run it, you just need useEffectEvent. #ReactJS #React19 #FrontendDevelopment #useEffect #useEffectEvent #JavaScript #ReactHooks #WebDevelopment #FrontendEngineer #CodeBetter #Performance #ModernReact #LearningInPublic

  • diagram

To view or add a comment, sign in

Explore content categories