Fixing stale React components with useEffect

Most React developers have written this at some point: ```js useEffect(() => { fetchUserData(userId); }, []); ``` It works — until it doesn't. The problem? You're telling React "run this once" but your effect actually depends on userId. When userId changes, your UI goes stale and you get bugs that are incredibly hard to trace. The fix is simple: ```js useEffect(() => { fetchUserData(userId); }, [userId]); ``` Always ask yourself: "What values does this effect read from the component scope?" Every one of them belongs in the dependency array. ESLint's exhaustive-deps rule will catch these automatically. If you're not using it, turn it on today. Small habits like this are what separate good developers from great ones. #ReactJS #JavaScript #WebDevelopment #Frontend

To view or add a comment, sign in

Explore content categories