🚀 React Re-Render 🔄 One of the most important concepts in React is Re-rendering. 1️⃣ What is Re-render? Re-render means: 👉 React updates the UI when state or props change 2️⃣ Simple Example function Counter() { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)}> {count} </button> ); } 👉 When you click the button: setCount updates state React re-renders the component UI updates with new value 3️⃣ What triggers Re-render? ✔ State change (useState) ✔ Props change ✔ Parent re-render 4️⃣ Important Myth ❌ 👉 “React reloads the whole page” 🚫 WRONG React only updates the changed parts of the UI using: 👉 Virtual DOM 5️⃣ Behind the Scenes State changes React creates new Virtual DOM Compares with old one (Diffing) Updates only changed elements 👉 This process is called Reconciliation 6️⃣ Problem: Unnecessary Re-renders 😬 Sometimes components re-render even when not needed. Example: <Child /> Even if props didn’t change, it may re-render when parent updates. 7️⃣ Solution ✅ Use: 👉 React.memo 👉 useMemo 👉 useCallback To optimize performance. 🔥 Key Takeaway Re-render is NOT bad ❌ Unnecessary re-render is the real problem ⚠️ #React #JavaScript #Frontend #WebDevelopment #LearningByDoing
React Re-Render: Understanding State and Props Changes
More Relevant Posts
-
If you think every re-render updates the DOM, this will change your mind. Rendering in React is just a function call. Your component runs, returns JSX, and React calculates what the UI should look like. That’s it. Now the important part 👇 Re-rendering does NOT mean the DOM was updated. It only means 👉 React called your component again The DOM is updated later and only if something actually changed. So the real flow is: Trigger → state or props change Render → React runs your component Commit → React updates only the differences This is why you can type inside an input, the component re-renders, and your text is still there. Because React doesn’t touch what hasn’t changed. The real optimization isn’t avoiding re-renders It’s understanding what happens after them. #React #FrontendDevelopment #JavaScript #WebDevelopment #SoftwareEngineering #ReactJS #FrontendEngineer #TechCareers #CodingTips #DeveloperMindset
To view or add a comment, sign in
-
-
POV: The Virtual DOM carries, you take the credit. 🤫😎 Have you ever wondered why apps built with React feel so smooth, even when the UI updates frequently? The secret lies in something called #DOM. Let's break it down in the simplest way possible. First, What is DOM? - DOM (Document Object Model) is basically a tree structure of your web page. 1. Traditional JavaScript 🐢 - When we make changes using plain JavaScript, the browser updates the entire section of the Real DOM. This process is slow and 'expensive' for performance. Think of it like repainting the entire house just to change a lightbulb. 2. The React Way: The Virtual DOM ⚡ - React uses a lightweight copy of the Real DOM, which is stored in memory. This is the Virtual DOM. The Process: a) In React, whenever we update something, it’s reflected in the Virtual DOM first. b) It then compares this updated copy with its previous version (this smart process is called Diffing). c) Finally, React updates only the parts that changed in the Real DOM. The Result: ✅ Faster performance ✅ Zero unnecessary updates #ReactJS #JavaScript #WebDevelopment #Frontend #CodingTips #TechCommunity #CodeCanvas
To view or add a comment, sign in
-
React does not update the UI when you call setState. Yes, that is correct. And that is exactly what makes React fast. Most developers assume that calling setState immediately updates the screen. But under the hood, React does something much smarter. It schedules It prioritizes It decides the best moment to update All of this happens because of an architecture called React Fiber. Before Fiber, React worked in a blocking way. Once rendering started, it could not be interrupted. The result was UI freezes, less fluid interactions, and poor performance in larger applications. With Fiber, everything changed. React can pause rendering React can prioritize more important updates such as user interactions React can break work into smaller chunks In practice, this means your application stays responsive even when a lot is happening at the same time. User interactions feel smooth, and heavy renders become almost unnoticeable. Here is the part most people miss. You do not control when rendering happens. React does. This changes how you should think about your code. You need to be more careful with side effects You need to understand batching You need to accept that updates are not always immediate If you have ever wondered why something did not update instantly, now you know. React is not just a UI library. It is an intelligent scheduler. Have you ever faced a bug caused by this behavior? #reactJS #javascript #React
To view or add a comment, sign in
-
-
Have you ever needed to touch the DOM directly while working in React? React is designed to be declarative. You describe the UI based on state, and the library handles the updates. But occasionally, you need what we call an 'escape hatch.' This is precisely where 'refs' come into play. They provide a way to access a DOM node or a React element directly, bypassing the standard re-render cycle. The most common reason to reach for a ref is to manage things like focus, text selection, or media playback. If you need a specific input field to focus the moment a page loads, you cannot easily do that with state alone. You need to reach into the DOM and call the native '.focus()' method. Beyond the DOM, refs are also perfect for storing any mutable value that needs to persist across renders without triggering a UI update. This includes things like timer IDs or tracking previous state values for comparison. However, with great power comes responsibility. Using refs is essentially stepping outside of the React ecosystem and taking manual control. If you can achieve your goal using props and state, you should. Overusing refs can make your components harder to debug and breaks the predictable flow of data that makes React so reliable. It is a tool for the edge cases, not the daily routine. Use it when you need to talk to the browser directly, but keep your core logic declarative. #ReactJS #SoftwareEngineering #WebDevelopment #Javascript #Frontend #CodingTips
To view or add a comment, sign in
-
Your component is re-rendered... Then what? Most developers stop thinking here. And that’s where the real magic starts. This is Post 3 of the series: 𝗥𝗲𝗮𝗰𝘁 𝗨𝗻𝗱𝗲𝗿 𝘁𝗵𝗲 𝗛𝗼𝗼𝗱 Where I explain what React is actually doing behind the scenes. React does NOT update the DOM immediately. Because touching the DOM is expensive. Instead, it follows a 3-step process: Render → Diff → Commit First, React creates something called a 𝗩𝗶𝗿𝘁𝘂𝗮𝗹 𝗗𝗢𝗠. Not the real DOM. Just a JavaScript object in memory. A blueprint of your UI. Every time your component runs, React builds a new tree of this Virtual DOM. And it already has the previous tree stored. Now comes the interesting part. React compares: Old tree vs New tree This step is called 𝗗𝗶𝗳𝗳𝗶𝗻𝗴. It finds exactly what changed. Not everything. Just the difference. Then comes the final step: 𝗖𝗼𝗺𝗺𝗶𝘁 phase React takes only those changes… …and updates the real browser DOM So no, React is NOT re-rendering your whole page. It’s doing surgical updates. The key insight: React separates thinking from doing. Thinking = Render + Diff Doing = Commit That’s why React is fast. Because touching the DOM is expensive. So React minimizes it. Flow looks like this: ✅️ Component runs ✅️ Virtual DOM created ✅️ Changes calculated ✅️ DOM updated ✅️ Browser paints UI Once you see this, you stop fearing re-renders. And start understanding them. In Post 4 of React Under the Hood: How does React actually compare trees? (The diffing algorithm 👀) Follow Farhaan Shaikh if you want to understand react more deeply. 👉 Read in detail here: https://lnkd.in/dTYAbM6n #React #Frontend #WebDevelopment #JavaScript #SoftwareEngineering
To view or add a comment, sign in
-
The DOM is officially a bottleneck. 🤯 Cheng Lou (former React core team) just dropped Pretext, and it is challenging how browsers have handled text layout for the past 30 years. For decades, figuring out how much space a paragraph occupies meant rendering it in the browser and measuring it. This triggers layout reflows (using tools like getBoundingClientRect), which is one of the most expensive and thread-blocking operations in web development. Enter Pretext: A pure JavaScript/TypeScript library that handles multiline text measurement without touching the DOM. Here is why it is so powerful: Instead of relying on CSS rendering, it uses an off-screen Canvas and a clever two-phase API (prepare() and layout()) to pre-calculate word sizes using pure arithmetic. The layout operations run in roughly 0.09ms. It natively supports platform-specific emojis, complex text directions (RTL), and different languages. It enables previously impossible UI effects, like text fluidly wrapping around moving, draggable obstacles in real-time. The project rocketed past 10,000 GitHub stars in just days because it solves a massive performance hurdle for the next generation of spatial and interactive UIs. #WebDevelopment #JavaScript #TypeScript #Frontend #SoftwareEngineering #TechNews #UIUX
To view or add a comment, sign in
-
Your UI lag is not always React. Sometimes… It’s the JavaScript event loop. Here’s what’s happening 👇 JavaScript is single-threaded. So when you run: → Heavy calculations → Large loops → Sync blocking code You block: ❌ Rendering ❌ User interactions Result: → UI freezes → Clicks feel delayed → App feels slow React can’t help here. Because it runs on the same thread. What works: ✔ Break work into chunks ✔ Use requestIdleCallback / setTimeout ✔ Offload heavy tasks (Web Workers) Key insight: Performance is not just “React optimization”. It’s understanding how the browser executes code. Ignore the event loop… And your UI will suffer. #JavaScript #EventLoop #Frontend #ReactJS #Performance #SoftwareEngineering #WebDevelopment #AdvancedReact #Engineering #Optimization
To view or add a comment, sign in
-
Why is React so fast compared to normal DOM manipulation?" 🤔 The answer lies in the Virtual DOM ⚡ Let’s break it down simply 👇 🔹 What is Virtual DOM? Virtual DOM is a lightweight copy of the real DOM. Instead of updating the real DOM directly, React first updates the Virtual DOM. 🔹 How does it work? 1. React creates a Virtual DOM 2. When state changes → new Virtual DOM is created 3. React compares it with the previous one (diffing) 4. Only the changed parts are updated in the real DOM 🔹 Why is it faster? 👉 Real DOM updates are slow 👉 Virtual DOM minimizes unnecessary updates 💻 Example concept: Instead of updating the entire UI, React updates only the part that actually changed. 🚀 Pro Tip: Efficient rendering is the reason React apps feel fast and smooth. 💬 Did you know this is how React optimizes performance? #reactjs #javascript #webdevelopment #mern #developers
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