Stop making this mistake in your useEffect! 🛑 If you are fetching data in React, you need to handle it like a pro. This image shows the difference between a simple fetch and a safe fetch. Here is the main problem: Junior Level: A basic fetch starts every time the ID changes. If the user clicks fast, multiple requests will run at the same time and can cause bugs. Pro Level: A professional uses an AbortController to stop the old request before starting a new one. Why should you use a cleanup function? It prevents "race conditions" where the wrong data might show up on your screen. It makes your app faster and avoids wasting network resources. You can easily cancel the fetch by calling controller.abort() in the return function. This small change makes your React projects much more stable and professional. Do you always use a cleanup function in your useEffect? Let me know! 👇 #ReactJS #WebDevelopment #CodingTips #CleanCode #FrontendDeveloper #JavaScript #Programming
Strong point. Cleanup with `AbortController` definitely makes fetch flows safer, but the real issue is usually not just “cancel the old request” — it is making async state transitions predictable.
Great post and great advice! What's more it applies to any action within useEffect. In general useEffect always should return a cleanup function, to avoid issues during rerender/unmounting.