Skip to content

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

### What if I need different logic for mount vs update??

Use ref: const isFirst = useRef(true); useEffect(() => { if(isFirst.current){isFirst.current=false; mountLogic();} else {updateLogic();} }, [deps]);

How to prevent double-fetching when props match??

Compare previous props manually or use caching layer. React Query handles this with request deduplication.

Should I use componentDidMount and componentDidUpdate in class components??

Compare prevProps in componentDidUpdate. In hooks deps handle this automatically but you lose prevValues without ref.

This guide is brought to you by DodaTech -- built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro