How to Fix useEffect Mount Logic Dependencies
DodaTech
Updated 2026-06-26
1 min read
The Problem
In this quick-fix, you'll learn how to structure effects that depend on specific prop values. This matters because initialization logic needing props must re-run on prop changes. In real-world applications, this pattern appears in profile data loading when userId changes map initialization on coordinates.
The Wrong Way
function ProfilePage({ userId }) {
const [profile, setProfile] = useState(null);
useEffect(() => { fetchProfile(userId).then(setProfile); }, []); // Missing userId
useEffect(() => { if(profile) fetchProfile(userId).then(setProfile); }, [userId]);
return <div>...</div>;
}
Two effects for same concern -- code duplication stale userId in first
The Right Way
function ProfilePage({ userId }) {
const [profile, setProfile] = useState(null);
useEffect(() => { fetchProfile(userId).then(setProfile); }, [userId]);
return <div>...</div>;
}
Single effect handles both mount and update
Prevention
- Put mount logic in same effect that responds to prop changes
- Include prop in deps for both mount and update
- Avoid separate mount and update effects
FAQ
This guide is brought to you by DodaTech -- built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
← Previous
How to Fix useEffect Interval Timer Issues
Next →
How to Fix Observable Subscription Leaks
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro