Deepak Singh Rawat’s Post

useEffect mistakes I made (and how I fixed them) ⚛️ Sharing real lessons from my React journey 👇 When I started learning React, useEffect felt simple… until it started breaking my app in ways I couldn’t explain 😅 Here are some real mistakes I made with useEffect — and what finally made it click: ❌ Mistake 1: Forgetting the dependency array At first, I wrote useEffect without dependencies and wondered why my API was getting called again and again. Fix: Always ask yourself: 👉 When should this effect run? Once → [] On state/prop change → [state] ❌ Mistake 2: Putting everything inside one useEffect I used to handle API calls, event listeners, and calculations inside a single useEffect. Fix: One effect = one responsibility Split effects based on what they do, not where they live. ❌ Mistake 3: Infinite re-render loop I updated state inside useEffect and added that same state to the dependency array. Boom 💥 infinite loop. Fix: Avoid unnecessary state updates Derive values when possible Double-check dependencies before adding them ❌ Mistake 4: Ignoring cleanup functions I forgot to clean up event listeners and timers — everything worked until performance tanked. Fix: Return a cleanup function: useEffect(() => { window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize); }, []); ❌ Mistake 5: Treating useEffect like componentDidMount I tried to do everything inside useEffect. Fix: Event handlers → stay in functions Derived values → useMemo Callbacks → useCallback useEffect is for side effects, not business logic. 💡 Final takeaway useEffect is powerful, but only when you understand why it runs, not just how to use it. If you’re learning React — these mistakes are normal. Debugging them is what actually levels you up 🚀 👉 What’s the biggest useEffect mistake you made? #ReactJS #JavaScript #FrontendDevelopment #WebDevelopment #LearningInPublic #ReactHooks

  • text

To view or add a comment, sign in

Explore content categories