🚀 Day 23 — #React useEffect (Updating Phase) Today I explored how useEffect works during the updating phase 👇 🧩 Example: Runs When Dependency Changes import { useState, useEffect } from "react"; function UpdateExample() { const [count, setCount] = useState(0); useEffect(() => { console.log("Count updated:", count); }, [count]); // Runs whenever count changes return ( <div> <h2>Count: {count}</h2> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); } export default UpdateExample; ✅ Key Points: 🔹 useEffect runs whenever the dependency (count) changes 🔹 Dependency array controls when the effect executes 🔹 Similar to componentDidUpdate in class components 💡 Use Cases: ✔ API calls on state change ✔ Syncing data with UI ✔ Triggering side effects based on user actions 🔥 Getting better at controlling React side effects step by step! #React #useEffect #FrontendDevelopment #JavaScript #LearningJourney 10000 Coders
React useEffect Updating Phase Explained
More Relevant Posts
-
We started naming our useEffect functions some time ago. Tiny change. Outsized impact. ❌ The "Mystery" Way useEffect(() => { fetch(`/api/users/${userId}`) .then(res => res.json()) .then(setUser); }, [userId]); Four effects like this in a component? You're reading every line just to understand what's happening. ✅ The "Self-Documenting" Way useEffect(function fetchUserProfile() { fetch(`/api/users/${userId}`) .then(res => res.json()) .then(setUser); }, [userId]); Now the name tells the story. You only open it when something breaks. But the real wins were unexpected: → If you can't name it without "and" — split it. "fetchUserAndLoadPermissions" is two effects in a trench coat. → Vague names expose effects that shouldn't exist. "updateStateBasedOnOtherState" isn't an effect. It's derived state. Move it out. → Stack traces go from useless to useful. "at (anonymous)" → no idea. "at fetchUserProfile" → straight to the bug. Want to enforce it across your team? "prefer-arrow-callback": ["error", { "allowNamedFunctions": true }] Zero new libraries. Zero new patterns. Just a name. So, start naming your effects. #React #JavaScript #FrontendDevelopment #WebDev
To view or add a comment, sign in
-
-
🎬 Movie Search Application (React) Developed a responsive movie search web application using React that allows users to search and explore movies in real time. The application integrates with an external movie API to fetch dynamic movie data such as title, poster, year. Implemented core React concepts including state management using hooks like useState and useEffect, along with event handling for search functionality. Added features like instant search, Enter key search, conditional rendering for error handling, and sorting movies by release year. Designed a clean and responsive UI using Flexbox and styled-components, including dark mode support for better user experience. 🔗 Live Demo https://lnkd.in/g2X-_Ken 💻 GitHub Repository https://lnkd.in/gczd-9bY #ReactJS #JavaScript #APIIntegration #RESTAPI #StyledComponents #ResponsiveDesign #FrontendDevelopment #WebDevelopment #MovieApp #FrontendProject #Coding #WebApp #Netlify #GitHub #OpenSource
To view or add a comment, sign in
-
🚀 𝗬𝗼𝘂𝗿 𝗥𝗲𝗮𝗰𝘁 𝗔𝗽𝗽 𝗜𝘀𝗻’𝘁 𝗦𝗹𝗼𝘄… 𝗬𝗼𝘂𝗿 𝗥𝗲-𝗥𝗲𝗻𝗱𝗲𝗿𝘀 𝗔𝗿𝗲. Most performance issues in React don’t come from “bad code”… They come from 𝘂𝗻𝗻𝗲𝗰𝗲𝘀𝘀𝗮𝗿𝘆 𝗿𝗲-𝗿𝗲𝗻𝗱𝗲𝗿𝘀. And this is where these 3 heroes step in 👇 👉 𝗥𝗲𝗮𝗰𝘁.𝗺𝗲𝗺𝗼 👉 𝘂𝘀𝗲𝗠𝗲𝗺𝗼 👉 𝘂𝘀𝗲𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸 🔍 𝟭. 𝗥𝗲𝗮𝗰𝘁.𝗺𝗲𝗺𝗼 Wraps a component and 𝗽𝗿𝗲𝘃𝗲𝗻𝘁𝘀 𝗿𝗲-𝗿𝗲𝗻𝗱𝗲𝗿𝗶𝗻𝗴 if props haven’t changed. 𝑐𝑜𝑛𝑠𝑡 𝑀𝑦𝐶𝑜𝑚𝑝𝑜𝑛𝑒𝑛𝑡 = 𝑅𝑒𝑎𝑐𝑡.𝑚𝑒𝑚𝑜(({ 𝑣𝑎𝑙𝑢𝑒 }) => { 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔("𝑅𝑒𝑛𝑑𝑒𝑟𝑒𝑑"); 𝑟𝑒𝑡𝑢𝑟𝑛 <𝑑𝑖𝑣>{𝑣𝑎𝑙𝑢𝑒}</𝑑𝑖𝑣>; }); ✅ 𝗨𝘀𝗲 𝘄𝗵𝗲𝗻: ● Component renders often ● Props rarely change 🔍 𝟮. 𝘂𝘀𝗲𝗠𝗲𝗺𝗼 Memoizes a value so it doesn’t recalculate on every render. 𝑐𝑜𝑛𝑠𝑡 𝑒𝑥𝑝𝑒𝑛𝑠𝑖𝑣𝑒𝑉𝑎𝑙𝑢𝑒 = 𝑢𝑠𝑒𝑀𝑒𝑚𝑜(() => { 𝑟𝑒𝑡𝑢𝑟𝑛 ℎ𝑒𝑎𝑣𝑦𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑖𝑜𝑛(𝑑𝑎𝑡𝑎); }, [𝑑𝑎𝑡𝑎]); ✅ 𝗨𝘀𝗲 𝘄𝗵𝗲𝗻: ● Computation is expensive ● You want to avoid recalculating 🔍 𝟯. 𝘂𝘀𝗲𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸 Memoizes a function so it doesn’t get recreated every render. 𝑐𝑜𝑛𝑠𝑡 ℎ𝑎𝑛𝑑𝑙𝑒𝐶𝑙𝑖𝑐𝑘 = 𝑢𝑠𝑒𝐶𝑎𝑙𝑙𝑏𝑎𝑐𝑘(() => { 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔("𝐶𝑙𝑖𝑐𝑘𝑒𝑑"); }, []); ✅ 𝗨𝘀𝗲 𝘄𝗵𝗲𝗻: ● Passing functions to child components ● Preventing unnecessary child re-renders ⚡ 𝗧𝗵𝗲 𝗥𝗲𝗮𝗹 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 ● 𝗥𝗲𝗮𝗰𝘁.𝗺𝗲𝗺𝗼 → Memoizes component rendering ● 𝘂𝘀𝗲𝗠𝗲𝗺𝗼 → Memoizes values ● 𝘂𝘀𝗲𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸 → Memoizes functions ⚠️ 𝗖𝗼𝗺𝗺𝗼𝗻 𝗠𝗶𝘀𝘁𝗮𝗸𝗲 ❌ Using them everywhere “just in case” 👉 They also add overhead ✔️ Use them only when performance issues exist 🔑 𝗙𝗶𝗻𝗮𝗹 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 ● Don’t optimize blindly. ● First understand re-renders → then optimize wisely. 🚀 Follow Shubham Kumar Raj for more such content. What’s your go-to optimization technique in React? 👇 #React #Frontend #WebDevelopment #Performance #JavaScript #FrontendRevisionMarathon #frontenddeveloper #codinginterview #programming #learnjavascript #interviewprep #CareerGrowth #SowftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 14 – setTimeout Gotcha Think setTimeout runs exactly after the delay? Not quite. 😏 Here’s a classic trap 👇 for (var i = 1; i <= 3; i++) { setTimeout(() => { console.log(i); }, 1000); } 👉 What do you expect? 1 2 3 ❌ 👉 What you get: 4 4 4 😲 💥 Why this happens var is function scoped, not block scoped By the time setTimeout executes, the loop is already done i becomes 4, so all callbacks log the same value ✅ Fix #1: Use let for (let i = 1; i <= 3; i++) { setTimeout(() => { console.log(i); }, 1000); } ✔️ Output: 1 2 3 ✅ Fix #2: Use Closure for (var i = 1; i <= 3; i++) { ((j) => { setTimeout(() => { console.log(j); }, 1000); })(i); } ✔️ Output: 1 2 3 🧠 Pro Insight (Angular Devs 👇) This matters a LOT when dealing with: async API calls loops with delayed UI updates timers inside components Using let or closures prevents unexpected UI bugs 🐛 ⚡ Takeaway 👉 setTimeout doesn’t pause your loop 👉 It schedules a callback for later 👉 Scope matters more than delay 💬 Have you ever been bitten by this bug? #JavaScript #Angular #WebDevelopment #AsyncJS #Frontend #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Frontend Performance — Learning in Public Over the last couple of days, I explored Lighthouse-based performance analysis on a real application. Key learnings 👇 🧠 Main Thread Matters 👉 Browser runs on a single thread 👉 Heavy JS → slower interaction 🚦 Render-Blocking Resources 👉 CSS/JS can delay UI rendering ⛓️ Critical Request Chain 👉 Too many dependencies → slower loading 📊 Lighthouse Insight 👉 It’s not about score 👉 It’s about identifying root causes 🎯 Big takeaway: Performance = reduce work + remove blockers 📌 Next: Building a real-world performance audit report #Frontend #WebPerformance #JavaScript #ReactJS #SoftwareEngineering #LearnInPublic
To view or add a comment, sign in
-
⚛️ React Concept: useEffect Explained Simply The "useEffect" hook lets you handle side effects in functional components — like API calls, subscriptions, and DOM updates. 🔹 It runs after the component renders 🔹 You can control when it runs using the dependency array Basic syntax: useEffect(() => { // side effect logic return () => { // cleanup logic (optional) }; }, [dependencies]); 📌 Common use cases: • Fetching data from APIs • Adding event listeners • Handling timers 📌 Best Practice: Always define dependencies correctly and use cleanup functions to avoid memory leaks. #reactjs #frontenddevelopment #javascript #webdevelopment #softwareengineering
To view or add a comment, sign in
-
-
useEffect is probably the most powerful - and most misused - hook in React. 🎯 Arun explained it really well, sharing this because I've made these exact mistakes in real projects: → Forgetting the cleanup function - memory leaks in production 😅 → Wrong dependency array - stale data showing up in dashboards → Fetching data inside useEffect - unnecessary re-renders and race conditions What changed for me: ✅ Always write cleanup for subscriptions and event listeners ✅ Use React Query for data fetching — avoids most useEffect complexity ✅ Think twice before adding objects/arrays as dependencies 2.5 years of React and useEffect still teaches me something new. What's your most common useEffect mistake? Drop it below 👇 #ReactJS #Frontend #JavaScript #WebDevelopment #FrontendDeveloper
Software Engineer | 3 years experience in Full Stack Web Development | React.js | JavaScript | Redux | Node.js | Express.js | Building Scalable & Performant Web Applications
⚛️ React Concept: useEffect Explained Simply The "useEffect" hook lets you handle side effects in functional components — like API calls, subscriptions, and DOM updates. 🔹 It runs after the component renders 🔹 You can control when it runs using the dependency array Basic syntax: useEffect(() => { // side effect logic return () => { // cleanup logic (optional) }; }, [dependencies]); 📌 Common use cases: • Fetching data from APIs • Adding event listeners • Handling timers 📌 Best Practice: Always define dependencies correctly and use cleanup functions to avoid memory leaks. #reactjs #frontenddevelopment #javascript #webdevelopment #softwareengineering
To view or add a comment, sign in
-
-
I was building a UI recently and something felt off. I added a console.log out of habit. Clicked a button once. Watched the same API call fire 10 to 11 times in a row. One click. Ten requests. I knew React re-renders components when state changes. What I didn't fully appreciate was how easy it is to accidentally create a chain reaction. A state update triggers a render, something inside that render looks "new" to React even when it isn't, and suddenly your useEffect is firing over and over. The fix came down to being more precise: stabilising object references with useMemo, wrapping functions in useCallback, and being honest about what my useEffect actually needed to watch. After fixing it: one click, one API call. It sounds obvious in hindsight. It always does. If you've hit this before, how did you catch it? Console.log is how I found mine. I'm told the React DevTools Profiler is better. That's what I'm learning next. React developers, how do you debug unexpected re-renders in production? #React #Frontend #FrontendDevelopment #ReactJS #WebDevelopment #JavaScript #SoftwareEngineering
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟏𝟖 𝐨𝐟 𝐦𝐲 𝟑𝟎-𝐃𝐚𝐲 𝐑𝐞𝐚𝐜𝐭.𝐣𝐬 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 🚀 Today I built a 𝗳𝘂𝗹𝗹𝘆 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗦𝘁𝗼𝗽𝘄𝗮𝘁𝗰𝗵 𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻 using the concept of 𝙪𝙨𝙚𝙎𝙩𝙖𝙩𝙚 𝙖𝙣𝙙 𝙪𝙨𝙚𝙍𝙚𝙛. This project helped me understand how to manage time-based logic and handle real-time updates efficiently in React. 𝗞𝗲𝘆 𝗰𝗼𝗻𝗰𝗲𝗽𝘁𝘀 𝗜 𝗽𝗿𝗮𝗰𝘁𝗶𝗰𝗲𝗱: - useState for managing time, laps, and running state - useRef to store interval without causing re-renders - setInterval & clearInterval for timer functionality - Functional state updates for accurate timing - Conditional rendering for Start / Pause controls - Handling arrays (Lap recording system) - Building a clean and responsive UI 𝗪𝗵𝗮𝘁 𝗜 𝗹𝗲𝗮𝗿𝗻𝗲𝗱: Managing real-time features like a stopwatch requires careful handling of intervals and state updates. Using 𝙪𝙨𝙚𝙍𝙚𝙛 for storing intervals was a key learning to avoid unnecessary re-renders. I also focused on creating a smooth user experience with lap tracking and a modern UI. Step by step, I’m getting closer to building production-level React applications. #ReactJS #FrontendDevelopment #WebDevelopment #JavaScript #ReactHooks #LearningInPublic #30DayReactChallenge
To view or add a comment, sign in
-
React Learning Series | Contd... #Day22: Difference between useEffect and useLayoutEffect Most developers use useEffect everywhere ⚛️ But very few truly understand when to use useLayoutEffect 🤔 Here’s the difference in one line 👇 🟢 useEffect → runs AFTER the UI is painted 🟠 useLayoutEffect → runs BEFORE the UI is painted This small timing difference can cause real UI issues 💥 Example: If you measure DOM size using useEffect, users may see a flicker 😬 Switching to useLayoutEffect fixes it instantly ⚡ Rule I follow 👇 ✅ 90% of cases → useEffect ⚠️ Use useLayoutEffect only when layout must be calculated before paint 🚨 Overusing useLayoutEffect can hurt performance since it blocks rendering React is not just about writing components… It’s about understanding when things happen ⏱️ Have you ever faced a UI flicker issue like this? #React #Frontend #JavaScript #WebPerformance
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development