useEffect Dependency Array Explained

❓ React Interview Question: What does the dependency array of useEffect affect? 💡 In React, the dependency array in useEffect controls when the effect runs. 🔹 1. No Dependency Array useEffect(() => { console.log("Runs on every render"); }); - runs after every render 🔹 2. Empty Dependency Array [] useEffect(() => { console.log("Runs only once"); }, []); - runs only once (on component mount) 🔹 3. With Dependencies [value] useEffect(() => { console.log("Runs when value changes"); }, [value]); - runs only when the dependency changes - it prevents unnecessary re-renders - improves performance - helps control side effects (API calls, subscriptions, timers) 💡 How to remember useEffect dependency array? no dependency array → runs on every render empty array [] → runs only once (on mount) array with values [value] → runs only when the value changes 👉 Follow Tarun Kumar for tech content, coding tips, and interview prep 🚀 #ReactJS #JavaScript #FrontendDevelopment #ReactHooks #useEffect #CodingTips #WebDevelopment #SoftwareEngineering #InterviewPreparation #Developers

To view or add a comment, sign in

Explore content categories