🎯 Are you overusing useEffect in React? I recently came across a brilliant decision tree that completely changed how I think about React hooks. Here's the game-changer: Before writing useEffect, ask yourself ONE question: "Is this syncing with an EXTERNAL system?" ✅ If YES → useEffect is fine ❌ If NO → You probably don't need it Here are the better alternatives: 📊 Transforming data? → Calculate during render (or use useMemo) 🖱️ Handling user events? → Use event handlers, not effects ⚡ Expensive calculation? → useMemo (not useEffect + setState) 🔄 Resetting state on prop change? → Use the `key` prop 📡 Subscribing to external store? → useSyncExternalStore The biggest mistake: Using useEffect to filter data or handle clicks. If you're doing this, there's a better way. Common anti-patterns to avoid: - useEffect + setState from props/state (causes extra re-renders) - useEffect for click/submit handlers (loses event context) - useEffect to notify parent components (breaks unidirectional data flow) When useEffect IS appropriate: - WebSocket connections - Third-party widget integration - Measuring DOM elements after render - Browser API subscriptions with cleanup Want to enforce this in your codebase? Check out the ESLint plugin: eslint-plugin-react-you-might-not-need-an-effect The React team's documentation on this topic is exceptional. Link in comments. 👇 What's your most common useEffect mistake? Drop it in the comments – let's learn together! #React #JavaScript #WebDevelopment #FrontendDevelopment #Programming #SoftwareEngineering #ReactJS #CodeQuality
Just a quick one, I recently built an app where I use Socket.io to track user event. I’m presently calling this inside useEffect. Do you consider this a good approach?
+ consider a library like React-Query to save yourself many problems Dan Neciu
The biggest mindset shift is realizing that if your logic fits in render, it should stay there. Effects are the escape hatch, not the default. Most bugs come from trying to manage state or handle events inside useEffect when React gives you better tools for those jobs.
If you are using useEffect to derive state from props or handle user intent, you are fighting the render model. That usually shows up later as extra renders, harder debugging, and brittle logic.
i was curious about the eslint plugin, you like it? no false positives?
Hey Dan Neciu what about the frontend security course you wanted to teach this new year
The "key" tip is really something interesting. Was unaware of it.
https://neciudan.dev/subscribe