𝐒𝐭𝐨𝐩 𝐏𝐫𝐨𝐩 𝐃𝐫𝐢𝐥𝐥𝐢𝐧𝐠! You have a user object at the top of your app, and you end up passing it through 5 different components—Layout, Page, Header, Sidebar—just so a tiny <Avatar /> at the very bottom can use it. By the time you're done, your middle components are "polluted" with props they don't even care about. It makes refactoring a nightmare and testing a chore. Before you reach for a heavy state management library like Redux or Zustand, try a simpler built-in pattern: 𝘊𝘰𝘮𝘱𝘰𝘯𝘦𝘯𝘵 𝘊𝘰𝘮𝘱𝘰𝘴𝘪𝘵𝘪𝘰𝘯. 𝐓𝐡𝐞 "𝐏𝐫𝐨𝐩 𝐃𝐫𝐢𝐥𝐥𝐢𝐧𝐠": <𝐀𝐩𝐩> → <𝐋𝐚𝐲𝐨𝐮𝐭 𝐮𝐬𝐞𝐫={𝐮𝐬𝐞𝐫}> → <𝐇𝐞𝐚𝐝𝐞𝐫 𝐮𝐬𝐞𝐫={𝐮𝐬𝐞𝐫}> → <𝐀𝐯𝐚𝐭𝐚𝐫 𝐮𝐬𝐞𝐫={𝐮𝐬𝐞𝐫} /> 𝐓𝐡𝐞 "𝐂𝐨𝐦𝐩𝐨𝐬𝐢𝐭𝐢𝐨𝐧" 𝐅𝐢𝐱: 𝐏𝐚𝐬𝐬 𝐭𝐡𝐞 𝐜𝐨𝐦𝐩𝐨𝐧𝐞𝐧𝐭 𝐢𝐭𝐬𝐞𝐥𝐟 𝐚𝐬 𝐚 𝐩𝐫𝐨𝐩 𝐨𝐫 𝐮𝐬𝐞 {𝐜𝐡𝐢𝐥𝐝𝐫𝐞𝐧}. Instead of passing data down, you "lift" the child component up. This means your Header doesn't even need to know the user exists—it just renders whatever you put inside it. Why I've started leaning into this: 𝐂𝐥𝐞𝐚𝐧𝐞𝐫 𝐂𝐨𝐝𝐞: No more "pass-through" props clogging up your components. 𝐓𝐫𝐮𝐞 𝐑𝐞𝐮𝐬𝐚𝐛𝐢𝐥𝐢𝐭𝐲: Your Header is now truly generic. 𝐄𝐚𝐬𝐢𝐞𝐫 𝐓𝐞𝐬𝐭𝐢𝐧𝐠: You can test each piece in total isolation. React isn't just about building components; it's about how you compose them. Keep your components "dumb" and your architecture smart. I'm curious—how many layers of prop drilling do you hit before you force a refactor? #ReactJS #WebDevelopment #SoftwareEngineering #CleanCode #JavaScript #FrontEnd
Reduce Prop Drilling in React with Composition
More Relevant Posts
-
🚀 𝐓𝐡𝐞 𝐌𝐨𝐦𝐞𝐧𝐭 𝐑𝐞𝐚𝐜𝐭 𝐂𝐨𝐧𝐭𝐞𝐱𝐭 𝐅𝐢𝐧𝐚𝐥𝐥𝐲 “𝐂𝐥𝐢𝐜𝐤𝐞𝐝” 𝐟𝐨𝐫 𝐌𝐞 I was building a simple dark/light theme feature. Sounds easy, right? Until my component tree started looking like this: App → Layout → Header → Toolbar → Button And I was passing theme through every single level. Even components that didn’t care about the theme had to accept it… just to pass it down. That’s when I realized — this isn’t scalable. This is prop drilling. 🧠 The Turning Point: React Context Instead of threading props manually through the tree, I used the Context API. Conceptually, Context works like a broadcast system: 1️⃣ createContext() → creates a shared container 2️⃣ <Provider> → supplies the value at a higher level 3️⃣ useContext() → consumes it anywhere below No more unnecessary prop forwarding. 🔬 𝐓𝐞𝐜𝐡𝐧𝐢𝐜𝐚𝐥 𝐃𝐞𝐭𝐚𝐢𝐥 𝐓𝐡𝐚𝐭 𝐌𝐨𝐬𝐭 𝐁𝐞𝐠𝐢𝐧𝐧𝐞𝐫𝐬 𝐌𝐢𝐬𝐬 React compares Provider values using Object.is. If you pass a new object literal like: <𝑃𝑟𝑜𝑣𝑖𝑑𝑒𝑟 𝑣𝑎𝑙𝑢𝑒={{ 𝑡ℎ𝑒𝑚𝑒: "𝑑𝑎𝑟𝑘" }} /> Every render creates a new reference → all consumers re-render. The better approach? Store the value in state Or memoize it This small detail makes a big difference in performance. 🎯 When Context Makes Sense ✔ Theme ✔ Auth state ✔ Language ✔ Global UI configuration But Context isn’t a replacement for props. It’s a tool for shared, cross-cutting state. The real lesson? Good React architecture isn’t about avoiding props. It’s about knowing when to elevate state and when to broadcast it. #ReactJS #FrontendDevelopment #JavaScript #ReactHooks #SoftwareArchitecture #WebDevelopment
To view or add a comment, sign in
-
-
Stop Wasting Re-renders: Debounce vs Throttle Have you ever built a search bar that sends an API request on every keystroke? Or added a scroll listener that suddenly makes the UI feel laggy? If you’re not controlling how often a function runs, you’re leaving serious performance gains on the table. Let’s simplify two powerful techniques every frontend developer should know. 👇 ⏱️ Debouncing — “Wait until things stop” Debouncing delays execution until the user stops triggering the event. Think of it like an elevator. It waits for the last person to enter. If someone else jumps in, the timer resets. The elevator moves only when no one else is coming. Best used for: 🔍 Search inputs (run API call after typing stops) 💾 Auto-saving drafts 📏 Window resize handlers 🌊 Throttling — “Run at a steady rate” Throttling ensures a function runs at most once every X milliseconds, no matter how often the event fires. Think of a dripping faucet. No matter how much water pressure there is, it releases one drop at fixed intervals. Best used for: 📜 Infinite scroll checks 🎮 Game controls 🖱️ Mouse / scroll animations 🔍 Quick Comparison Feature Debounce Throttle Goal Run after activity stops Run at fixed intervals Best for Input-heavy events Continuous events Execution Once (after pause) Multiple times (spaced out) 🛠️ Pro Tip In production, don’t reinvent the wheel. Libraries like Lodash make this extremely simple: import { debounce, throttle } from "lodash"; Or build custom React hooks to keep your components clean and optimized. 💬 Question for developers: Which one do you use more in real projects — Debounce or Throttle? Drop your answer in the comments 👇 #WebDevelopment #ReactJS #JavaScript #FrontendDevelopment #PerformanceOptimization #CodingTips #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 11/30 – useRef Hook (Deep Dive) Today I learned one of the most underrated React Hooks ⚡ 👉 useRef Today I learned: ✅ useRef stores a mutable value that persists across renders ✅ Updating useRef does NOT trigger re-render ✅ Mostly used for DOM access & storing values 💻 Example: import { useRef } from "react"; function InputFocus() { const inputRef = useRef(); const focusInput = () => { inputRef.current.focus(); }; return ( <> Focus </> ); } 🔥 What actually happens behind the scenes: 1️⃣ Component renders → ref object is created 2️⃣ React attaches it to DOM → inputRef.current 3️⃣ Value stays same across renders 👉 No re-render happens when ref changes 💡 useRef vs useState (Important Difference): useState → triggers re-render 🔁 useRef → does NOT trigger re-render ⚡ 💡 Real Use Cases: Focus input fields Store previous values Access DOM elements Avoid unnecessary re-renders ⚡ Advanced Insight: useRef is like a “box” that React does NOT track for rendering. 🔥 Key Takeaway: useRef is not for UI updates — it’s for storing values & accessing DOM without re-render. Are you using useRef or still confused? 👇 #React #useRef #Hooks #FrontendDevelopment #JavaScript #LearnInPublic #CodingJourney
To view or add a comment, sign in
-
-
I wrote the same component 3 times before I realised I only needed to write it once. Days 43–45/100 — 3 days deep into React, and the foundations are finally locking in. Here's everything that clicked this week: 🔹 Fragments — the invisible wrapper React needs a single parent element. But wrapping everything in a <div> pollutes your DOM. <> </> groups your JSX with zero DOM footprint. Clean markup. No ghost divs. No unexpected CSS side effects. 🔹 Map method — lists without the pain In vanilla JS, rendering a list meant loops + manual DOM updates. In React — one .map() call and every item in your array becomes a component. Dynamic. Clean. Automatic. 🔹 Conditional Rendering — 3 tools, 1 goal Making React show the right thing at the right time: • if-else — for complex logic outside JSX • Ternary — clean inline decisions inside JSX • Logical && — show something only when a condition is true Your UI should respond to data — not just display it. 🔹 Passing data via Props Props are how components talk to each other. Parent has data → passes it down → child receives and uses it. Without props, every component is an island. With props, they become a system. 🔹 Destructuring Props Instead of props.name, props.age, props.email everywhere — const { name, age, email } = props. Cleaner. Less repetitive. Readable at a glance. The biggest realisation from these 3 days? A component isn't a fixed UI block. It's a template. Props are the variables. Conditions are the logic. Map is the engine. That's the foundation of almost every React app in production. Day 45 done. 55 to go. 🚀 #100DaysOfCode #ReactJS #MERNStack #WebDevelopment #LearningInPublic #JavaScript #BuildInPublic
To view or add a comment, sign in
-
I stared at my console for 2 hours. useEffect was running twice. I had no idea why. Turns out — I didn't understand React at all. 😅 After 4 years as a Frontend Developer, I still see this confuse people daily: ❌ useEffect running on every render ❌ Why it runs TWICE in development ❌ Memory leaks from missing cleanup ❌ API calls failing when component unmounts ❌ That ESLint warning that won't go away So I wrote a complete visual guide breaking all of it down. The 3-question mental model that fixed it for me: → What do I want to do? → When should it run? → Do I need to clean up? That's it. Every useEffect problem comes down to these 3 questions. Full article with code examples + visual diagram 👇 https://lnkd.in/gqcTEcGM #React #JavaScript #WebDevelopment #Frontend #100DaysOfCode
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
-
Building the Frontend for My Movie Application 🎬 Today I focused on implementing the frontend interface for the movie application I’ve been working on. The goal was to transform the backend movie data into a structured and responsive UI so users can browse content easily. Here are the parts implemented so far: 🔹 Hero Section (Featured Movie) A large banner displaying the selected movie with title, description, and quick action buttons. 🔹 Trending Movies Section Rendering movie cards dynamically in a horizontal layout for easy browsing. 🔹 Dynamic Data Rendering Movie data is fetched from the API and rendered directly in the interface instead of using static content. 🔹 Movie Cards Component Each card displays poster image, movie title, and rating to provide a quick overview. 🔹 Responsive Layout UI components structured to maintain consistency across different screen sizes. This frontend structure helps connect the API layer with the user interface and prepares the foundation for upcoming features like detailed movie pages and interaction components. . . . . #ReactJS #JavaScript #FrontendDevelopment #NodeJS #ExpressJS #MERNStack #WebDevelopment #SoftwareEngineering #FullStackDevelopment #BuildInPublic
To view or add a comment, sign in
-
-
👉Understanding the useRef Hook in React React provides powerful Hooks to manage components. 👉 One important hook is useRef, which allows you to access and store values without re-rendering the component. 1. What is useRef? Answer: i).useRef is a React Hook used to reference DOM elements or store values that persist between renders. ii).In simple words It helps you directly access elements or remember values without refreshing the UI. 👉Syntax: const refName = useRef(initialValue); 2. Why use useRef? ✔ Access DOM elements (input, button, etc.) ✔ Store previous values ✔ Avoid unnecessary re-renders ✔ Manage focus, animations, and timers 👉 Example: Focus Input Field import React, { useRef } from "react"; function FocusInput() { const inputRef = useRef(); const handleFocus = () => { inputRef.current.focus(); }; return ( <div> <input ref={inputRef} type="text" placeholder="Enter your name" /> <button onClick={handleFocus}>Focus Input</button> </div> ); } export default FocusInput; --> How it works? • useRef creates a reference • ref connects to input element • Clicking button focuses input field • No re-render happens ✴️Key Difference: useState vs useRef -------------------------------------------------------------- • useState → causes re-render • useRef → does NOT cause re-render -------------------------------------------------------------- #React #ReactJS #WebDevelopment #FrontendDevelopment #JavaScript #Programming #SoftwareDevelopment #Learning
To view or add a comment, sign in
-
-
For a long time, I focused on stopping React from re-rendering. I used "React.memo", "useCallback", and every optimization technique I could find — all to reduce renders. Then I realized something important: Re-renders aren’t usually the real problem. React is actually very fast at rendering. The real issue is doing heavy work during the render phase. For example: const sortedUsers = users.sort((a, b) => a.age - b.age) This sorting runs on every render, which can become expensive as the data grows. A better approach: const sortedUsers = useMemo( () => users.slice().sort((a, b) => a.age - b.age), [users] ) Now the computation only runs when "users" actually changes. The goal isn’t fewer renders. The goal is less work per render. That small mindset shift completely changed how I think about React performance and optimization. Curious to hear from other developers: When did React finally “click” for you? #ReactJS #JavaScript #FrontendDevelopment #WebDevelopment #SoftwareEngineering #Programming #Coding #TechLearning #ReactPerformance #SourceCodeDev
To view or add a comment, sign in
-
Explore related topics
- How to Refactor Code Thoroughly
- How Developers Use Composition in Programming
- How to Achieve Clean Code Structure
- How to Refactor Code After Deployment
- How to Resolve Code Refactoring Issues
- Coding Best Practices to Reduce Developer Mistakes
- How to Improve Code Maintainability and Avoid Spaghetti Code
- How to Add Code Cleanup to Development Workflow
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
I don’t use react a lot but rather vue , where I would just use provide/inject pattern. I think the equivalent here is context api in react no?