MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
The Dependency Array and Execution Lifecycle0%
Study Materials
React.js for BeginnersProgress 0%
1.1Why React and Single-Page Architecture1.2Setting Up Modern React with Vite1.3React Project Directory Structure and Entry Points1.4Virtual DOM and React Render Cycle Explained
8.1Rules of React Hooks8.2The useEffect Hook for Side Effects8.3The Dependency Array and Execution Lifecycle8.4Cleanup Functions in useEffect
HomeTutorialsReact.js for BeginnersThe Dependency Array and Execution Lifecycle
Cleanup Functions in useEffect

The Dependency Array and Execution Lifecycle

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. Controlling Effect Execution

The second argument passed to useEffect is the Dependency Array. This array tells React exactly which state variables or props the effect relies on.

React evaluates the dependency array using shallow equality (Object.is) before every re-render to determine whether the effect callback should run again.

1Does dependencyArray exist?
2 ├─ NO ──> Runs on INITIAL MOUNT and AFTER EVERY RE-RENDER
3 │
4 ├─ YES: [] (Empty) ──> Runs ONLY ONCE on INITIAL MOUNT
5 │
6 └─ YES: [a, b] ──> Runs on INITIAL MOUNT and ONLY WHEN 'a' OR 'b' CHANGES

2. The 3 Dependency Array Modes

Mode 1: No Dependency Array (Run on Every Render)

1useEffect(() => {
2 console.log('Component rendered or re-rendered!');
3});
4// ⚠️ WARNING: If you update state inside an effect with no dependencies,
5// you will create an INFINITE RENDER LOOP!

Mode 2: Empty Dependency Array [] (Run on Mount Only)

1useEffect(() => {
2 console.log('Component mounted to screen!');
3 fetchInitialAnalytics();
4}, []);
5// Runs once when the component first appears on the screen.

Mode 3: Specific Dependencies [dep1, dep2] (Run on Change)

1useEffect(() => {
2 console.log(`User ID changed to: ${userId}. Fetching user profile...`);
3 fetchUserProfile(userId);
4}, [userId]);
5// Runs on mount AND whenever the value of userId changes.

3. The Golden Rule of Dependencies

Every reactive value referenced inside your effect callback MUST be included in the dependency array.

A "reactive value" includes:

  • Props
  • State variables
  • Variables or functions declared inside the component body
1function SearchResults({ query }) {
2 const [filter, setFilter] = useState('all');
3 
4 useEffect(() => {
5 // 'query' comes from props; 'filter' comes from state.
6 // Both MUST be in the dependency array!
7 fetchResults(query, filter);
8 }, [query, filter]); // ✅ All reactive values included!
9}

4. The Pitfall of Objects and Functions in Dependencies

Because React checks dependencies using reference equality (Object.is), defining objects or functions inside your component body can trigger unwanted effect runs:

1function Dashboard() {
2 // ❌ Problem: options is a brand new object reference on EVERY render!
3 const options = { theme: 'dark' };
4 
5 useEffect(() => {
6 applyTheme(options);
7 }, [options]); // Effect runs on EVERY render because options reference changes!
8 
9 // ✅ Solution: Move static objects outside the component, or use useMemo
10}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What happens if you omit the dependency array entirely in useEffect(() => { ... })?
Question 2
What happens if you update a state variable inside an effect that has NO dependency array?
Question 3
What comparison mechanism does React use to check if dependencies have changed?
Question 4
Which variables MUST be included in the useEffect dependency array?
Question 5
Why can declaring an object literal inside a component body and passing it to a dependency array cause an effect to run repeatedly?
Next Lesson

Cleanup Functions in useEffect

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
The useEffect Hook for Side EffectsCleanup Functions in useEffect

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext