REACT INTERNALS - PART 4 What Actually Triggers a Re-render? In the previous parts, we saw: 1. Rendering recalculates UI 2. Reconciliation compares elements 3. Keys preserve identity Now the real question: When does React decide to render again? 🔥 The 4 Main Re-render Triggers 1️⃣ State Updates setCount(count + 1) When state changes, the component function runs again. Important detail: The component re-runs - not necessarily the entire DOM updating. Rendering ≠ DOM mutation. 2️⃣ Parent Re-renders If a parent component re-renders, its children re-render too. Unless they are memoized (React.memo) and their props are referentially stable. This is why component boundaries matter. 3️⃣ Props Reference Changes React compares props using shallow comparison. <Child config={{ theme: "dark" }} /> A new object is created on every render → new reference → child re-renders. Even if the values look identical. React compares references, not deep values. 4️⃣ Context Value Changes When a context value changes: All consuming components re-render. Even if they use only part of that value. Context widens the render scope. 🧠 Important Mental Model React re-renders when: 1. State changes 2. Parent renders 3. Props reference changes 4. Context updates React does not re-render when: 1. Variables change 2. You mutate objects silently 3. You update the DOM manually React responds to its own state system - not external mutations. 🎯 Why This Matters Performance issues usually aren’t caused by React. They’re caused by: 1. Poor state placement 2. Unstable references 3. Large render trees Understanding what triggers a re-render gives you control. And control is where performance architecture begins. #ReactJS #FrontendDevelopment #JavaScript #ReactInternals #SoftwareEngineering
React Re-render Triggers: State, Parents, Props, Context
More Relevant Posts
-
🧠 I thought I understood the Intersection Observer API… until I looked under the hood. Like most frontend developers, I’ve used it for things like: • Lazy loading images • Infinite scrolling • Triggering animations when elements enter the viewport But today I spent some time digging deeper into how it actually works inside the browser, and it’s surprisingly elegant. Traditionally, many scroll-based implementations rely on listening to scroll events and repeatedly calculating element positions relative to the viewport. The issue is that this can run very frequently during scrolling, which may trigger layout calculations and impact performance. Intersection Observer approaches this differently. Instead of us constantly checking element visibility, the browser internally tracks intersections and notifies us only when specific thresholds are crossed. A few things that stood out to me: ⚡ Observers trigger only when visibility thresholds change ⚡ Multiple elements can share a single observer instance ⚡ The browser can batch and optimize intersection calculations internally It’s one of those APIs that feels simple on the surface but becomes really impressive when you look at the design behind it. Frontend engineering never stops surprising me 🌐 💬 What’s a Web API you explored recently that felt simple at first but turned out to be deeply engineered? #frontend #javascript #webperformance #webdevelopment #browser
To view or add a comment, sign in
-
𝐈𝐬 𝐲𝐨𝐮𝐫 𝐑𝐞𝐚𝐜𝐭 𝐜𝐨𝐦𝐩𝐨𝐧𝐞𝐧𝐭 𝐫𝐞-𝐫𝐞𝐧𝐝𝐞𝐫𝐢𝐧𝐠 𝐭𝐨𝐨 𝐨𝐟𝐭𝐞𝐧 𝐟𝐨𝐫 𝐧𝐨 𝐠𝐨𝐨𝐝 𝐫𝐞𝐚𝐬𝐨𝐧? Many devs instinctively reach for `useState` to store any value that changes, even if that change doesn't directly need to update the UI. This often leads to unnecessary re-renders, especially in complex components. That's where `useRef` shines beyond just DOM manipulation. `useRef` is your go-to for holding any mutable value that needs to persist across renders without triggering a re-render when its `.current` property is updated. Think about timers, previous props/state, or any value that needs to live for the component's lifetime but isn't part of its render output. Here's a quick example: ```javascript import React, { useRef, useEffect } from 'react'; function TimerComponent() { const countRef = useRef(0); // This value won't trigger re-renders useEffect(() => { const intervalId = setInterval(() => { countRef.current += 1; console.log('Current count (internal):', countRef.current); // You could update a useState here if you wanted to display it }, 1000); return () => clearInterval(intervalId); }, []); // Empty dependency array ensures it runs once return ( <div> <p>Timer running in console, UI isn't re-rendering on count updates.</p> </div> ); } ``` This approach keeps your component efficient by only triggering renders when the UI actually needs to change, preventing performance bottlenecks in larger applications. It's a subtle but powerful optimization. How do you typically manage mutable, non-UI state in your React components? Let's discuss performance tips! #React #FrontendDevelopment #Performance #JavaScript #WebDev
To view or add a comment, sign in
-
⚛️ 𝗣𝗿𝗲𝘃𝗲𝗻𝘁𝗶𝗻𝗴 𝗨𝗻𝗻𝗲𝗰𝗲𝘀𝘀𝗮𝗿𝘆 𝗥𝗲-𝗿𝗲𝗻𝗱𝗲𝗿𝘀 𝗶𝗻 𝗥𝗲𝗮𝗰𝘁 — 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗮𝗹 𝗨𝘀𝗲 𝗼𝗳 𝗺𝗲𝗺𝗼 & 𝘂𝘀𝗲𝗖𝗮𝗹𝗹𝗯𝗮𝗰𝗸 In React applications, performance issues often come from repeated re-renders rather than heavy logic. This becomes more noticeable when passing functions as props. 𝗖𝗼𝗺𝗺𝗼𝗻 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 <ChildComponent onClick={() => handleClick(id)} /> A new function is created on every render, causing child components to re-render. 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗮𝗹 𝗙𝗶𝘅 Use useCallback: const handleItemClick = useCallback((id) => { handleClick(id) }, []) Then: <ChildComponent onClick={handleItemClick} /> 𝗖𝗼𝗺𝗯𝗶𝗻𝗲 𝘄𝗶𝘁𝗵 𝗥𝗲𝗮𝗰𝘁.𝗺𝗲𝗺𝗼 export default React.memo(ChildComponent) Now child re-renders only when props actually change. 𝗪𝗵𝗲𝗻 𝗧𝗵𝗶𝘀 𝗛𝗲𝗹𝗽𝘀 𝗠𝗼𝘀𝘁 • Large lists • Dashboard UI • Reusable components • Expensive child rendering • Deep component trees 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗮𝗹 𝗡𝗼𝘁𝗲 Avoid over-optimizing: • Use memo only where needed • Measure before optimizing • Keep code readable Small memoization changes can improve UI responsiveness significantly. 💬 Curious — Do you use memoization by default, or only after noticing performance issues? #ReactJS #Frontend #Performance #JavaScript #WebDevelopment #SoftwareEngineering #DevCommunity
To view or add a comment, sign in
-
Most React projects don't fail because of React. They fail because of the libraries built around it. The ecosystem is vast — and choosing the wrong state management tool, UI library, or form handler in week one creates compounding technical debt that slows down every sprint after. In the guide below, we break down the 10 best React libraries and when each one actually fits. • Redux — still the gold standard for complex, shared state across large applications • MobX — reactive state management for teams that want less boilerplate • Material UI — the fastest path to a polished, consistent UI, but opinionated • React Bootstrap — familiar grid system, good for teams migrating from traditional web • Framer Motion — animation that integrates cleanly without fighting your component structure • Formik — form state and validation that scales beyond simple contact forms • React Hook Form — lighter than Formik, better performance on large forms • React Router — the default for client-side navigation, with good reason At Monocubed, we see the same pattern with engineering leads and CTOs: they do not just want a list of popular libraries. They want to know which combination will still make sense at 10x the current codebase size. If you are building or scaling a React application, this guide will help you think through your stack decisions: • Are your library choices driven by the current team's familiarity — or by what the application actually needs? • Where does your current state management approach start to break down? • Is your UI library helping you move faster, or has it become a constraint? Read the full guide and tell us in the comments: which React library has saved your team the most time? https://lnkd.in/dPSr-wVJ #Monocubed #React #WebDevelopment #JavaScript #Frontend #TechStrategy
To view or add a comment, sign in
-
💡 What is DOM? (And Why Every Frontend Developer Must Truly Understand It) When we write HTML, we’re not just creating a webpage. We’re creating a structure that the browser converts into something powerful: the DOM (Document Object Model). 📌 So what exactly is the DOM? The DOM is a programming interface that represents your HTML document as a tree of objects (nodes). Every: <html> tag <body> tag <div> <button> even text inside elements … becomes a node inside a structured tree. This allows JavaScript to: ✔ Read content ✔ Modify elements ✔ Change styles ✔ Add or remove nodes ✔ Respond to user events Without the DOM, JavaScript couldn’t dynamically update a webpage. 🧠 How It Actually Works Behind the Scenes 1️⃣ Browser parses HTML 2️⃣ It creates a DOM tree in memory 3️⃣ JavaScript interacts with this tree 4️⃣ When changes happen, the browser repaints the UI Every DOM manipulation has a performance cost — especially large or frequent updates. This is exactly why libraries like React introduced the concept of a Virtual DOM — to minimize direct DOM operations and optimize rendering. ⚡ Why Understanding DOM is Important (Even If You Use React) Even if you work with modern frameworks: Re-renders are still tied to DOM updates Performance optimization depends on understanding repaint & reflow Debugging UI issues often requires DOM inspection Event bubbling & capturing are pure DOM concepts If you truly understand the DOM, you understand frontend at a deeper level. #WebDevelopment #FrontendDevelopment #JavaScript #HTML #DOM #Programming #Coding #ReactJS #FrontendEngineer #SoftwareEngineering #WebDev #LearnToCode #DeveloperLife #CodingJourney #TechCommunity #ContinuousLearning
To view or add a comment, sign in
-
-
🚀 Stop Re-rendering Your Components! Understanding useRef in React If you are coming from a JavaScript background, you might be used to grabbing DOM elements using document.getElementById(). In React, we have a much cleaner, more powerful way to handle this: the useRef hook. 🔍 What is useRef? Think of useRef as a "secret storage box" that stays with your component. You can put a value in it, and it will stay there even when the component re-renders. The magic part? Changing the value inside a ref does NOT trigger a re-render. 🏗️ Why is it Important? In React, using useState is great for things that should update the UI (like a counter or a form input). But sometimes, you need to store data without refreshing the screen. ----> This is where useRef shines: Direct DOM Access: Need to focus an input field, scroll to a specific element, or trigger an animation? useRef gives you direct access to the DOM node. Storing Mutable Values: You can keep track of previous state values or timer IDs without causing unnecessary performance overhead. Performance Optimization: Since it doesn't trigger a re-render, it’s much faster for background calculations or tracking variables. 💻 How to Use It Key Takeaway Use useState if the information is used for rendering the UI. Use useRef if you need to "remember" something or interact with the DOM directly without affecting the render cycle. Devendra Dhote Ritik Rajput Are you using useRef in your projects? Let’s discuss in the comments! 👇 #ReactJS #WebDevelopment #Frontend #CodingTips #JavaScript #MERNStack
To view or add a comment, sign in
-
-
Ever wondered… if JavaScript already lets us directly manipulate the DOM, why do modern frameworks avoid doing that? 🤔 When I started, I used to think: “Why not just use document.querySelector() and change things directly?” But frameworks like Angular, React, Vue, and Svelte take a different path—and for good reason. 👉 Here’s the catch with direct DOM manipulation: • It’s imperative (you tell the browser how to update step-by-step) • It becomes hard to maintain as apps grow • It can lead to unexpected bugs when multiple updates collide • It tightly couples your logic with the UI structure ⸻ 💡 So what do frameworks do instead? They introduce controlled abstractions: 🔹 React → useRef Gives access to DOM nodes only when necessary, while keeping rendering declarative. 🔹 Angular → ViewChild / Renderer2 • ViewChild lets you safely access elements inside Angular’s lifecycle • Renderer2 ensures DOM updates are platform-safe (important for SSR, web workers, etc.) 🔹 Vue / Svelte → Refs & reactive bindings They rely heavily on reactivity, updating the DOM efficiently without you touching it directly. ⸻ 🚀 Why this approach wins: ✔ Declarative UI → You describe what the UI should look like ✔ Better performance → Virtual DOM / fine-grained reactivity ✔ Maintainability → Cleaner, predictable code ✔ Cross-platform compatibility → Not tied to browser-only APIs ⸻ ⚠️ But here’s the truth: Direct DOM manipulation is not wrong—it’s just something you should use sparingly. Use it when: • Working with third-party libraries (charts, maps) • Managing focus, scroll, or measurements • Handling edge-case performance tweaks ⸻ 💬 In simple terms: Direct DOM manipulation is like manual driving 🚗 Framework abstractions are like cruise control 🚀 Both are useful—but one scales much better. ⸻ Curious—what was your biggest “aha moment” when learning this? 👇 #JavaScript #WebDevelopment #React #Angular #Vue #Svelte #Frontend #SoftwareEngineering
To view or add a comment, sign in
-
𝗜𝗻𝗹𝗶𝗻𝗲 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗶𝗻 𝗥𝗲𝗮𝗰𝘁 𝗔𝗿𝗲 𝗡𝗼𝘁 𝗔𝗹𝘄𝗮𝘆𝘀 𝗮 𝗣𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 You’ve probably heard this before: “Don’t use inline functions in JSX. It causes re-renders.” Example: <button onClick={() => setCount(count + 1)}> Increment </button> Yes — this creates a new function on every render. But here’s the important part: Creating a new function is usually very cheap. In most applications, this is not your performance bottleneck. The real issue happens when: • The function is passed to a memoized child • The child relies on reference equality • The component tree is large For example: <Child onClick={() => setCount(count + 1)} /> If the child is wrapped with React.memo, the changing function reference can trigger re-renders. That’s when useCallback might make sense. But adding useCallback everywhere “just in case” adds complexity. A better rule: 📌 𝙊𝙥𝙩𝙞𝙢𝙞𝙯𝙚 𝙬𝙝𝙚𝙣 𝙞𝙩 𝙖𝙛𝙛𝙚𝙘𝙩𝙨 𝙗𝙚𝙝𝙖𝙫𝙞𝙤𝙧 — 𝙣𝙤𝙩 𝙗𝙚𝙘𝙖𝙪𝙨𝙚 𝙖 𝙧𝙪𝙡𝙚 𝙨𝙖𝙮𝙨 𝙨𝙤. Most performance issues in React are architectural, not about small function allocations. Day 11/100 — sharing practical frontend engineering lessons. Do you avoid inline functions, or only optimize when needed? #ReactJS #FrontendEngineering #JavaScript #WebPerformance #SoftwareArchitecture
To view or add a comment, sign in
-
Just tried something new in frontend… and I’m kinda liking it 😄 I recently started using shadcn/ui in a real React project, and honestly—it feels different. In a good way. As someone still growing in frontend, I remember how confusing UI libraries used to feel… 👉 Too many props 👉 Theme overrides everywhere 👉 Docs open in 10 tabs just to change one color 😅 But this time? It felt… simple. I ran a CLI command, got actual components inside my project, and suddenly I wasn’t using a UI library… I was working with my own UI code. Here’s what clicked for me 👇 🚀 No more black box The components are right there in my /components folder. I can read them, tweak them, break them (and fix them 😄). 🎨 Consistent design without stress Buttons, forms, dialogs—everything just looks like it belongs together. No random styling battles. 🧩 Feels scalable (even for beginners) I’m not fighting the library. I’m slowly understanding how to build my own UI layer on top of it. Honestly, the biggest change isn’t the UI… It’s the feeling of control. For the first time, frontend doesn’t feel like “adjusting someone else’s system”— It feels like I’m building my own. 💻✨ Curious— Are you still installing UI libraries… or starting to own your UI as well? 👇 #ReactJS #FrontendDevelopment #shadcnui #TailwindCSS #WebDevJourney
To view or add a comment, sign in
-
🚀React’s Virtual DOM When I started learning React, one concept that completely changed how I think about UI performance was the Virtual DOM. Instead of updating the real DOM directly, React creates a lightweight copy of it in memory called the Virtual DOM. Here’s how it works: 🔹 Render Virtual DOM React first creates a virtual representation of the UI using JavaScript objects. 🔹 State Change When the application state changes, React creates a new Virtual DOM tree. 🔹 Diffing React compares the new tree with the previous one using an efficient diffing algorithm. 🔹 Update Real DOM Only the necessary changes are applied to the actual DOM. 💡 Why this matters • Faster UI updates • Avoids full DOM re-rendering • Efficient reconciliation process • Batched state updates • Better performance for complex apps The result: smarter rendering and faster applications. Frontend engineering becomes much easier when you understand what happens behind the scenes. What concept in React took you the longest to understand? 👇 #React #JavaScript #WebDevelopment #FrontendDevelopment #SoftwareEngineering
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