A small React question 👇 //child component const Child = React.memo(({ name }) => { console.log("Child rendered"); return <div>{name}</div>; }); Parent component renders this: <Child name="Vivek" /> If the parent re-renders, will the child render again? ✅ No. Because React checks: "Vivek" === "Vivek" So React.memo skips the render. → Now a tiny change: <Child config={{ theme: "dark" }} /> Same question. Will the child render again? 👀 ➡️ Yes. Even though the object looks identical. Because in JavaScript: { theme: "dark" } !== { theme: "dark" } A new object is created on every render, so React thinks the prop changed. The fix is simple: const config = useMemo(() => ({ theme: "dark" }), []); <Child config={config} /> Now the reference stays the same and React.memo can actually skip the render. One of the biggest React performance lessons: Strings and numbers are usually safe. Objects, arrays, and functions can quietly break memoization. So if you want to avoid unnecessary re-renders in child components, be careful when passing objects as props. #react #javascript #frontend #webdevelopment #fullstackdevelopment #softwaredeveloper #fullstackdeveloper
React Memoization with Objects and Arrays
More Relevant Posts
-
7 React Hooks You Must Master React Hooks replaced class components and changed how every React developer thinks about state, side effects, and performance. Master these 7 and you can handle 95 percent of React challenges. -> useState Your component's memory. Stores local state and triggers a re-render when it changes. The most used hook in React by a significant margin. -> useEffect Handles everything that happens outside the render cycle. API calls, subscriptions, timers, and DOM manipulation all live here. Runs after every render unless you control its dependency array. -> useContext Access global state without passing props through every level of the component tree. Works perfectly with the Context API for theme, auth, and shared configuration. -> useRef Two jobs: access DOM elements directly without a re-render, and store values that persist across renders without causing re-renders. More powerful than most developers realize when they first learn it. -> useMemo Memorizes the result of an expensive calculation. Only recalculates when its dependencies change. Use it when a computation is genuinely expensive and runs on every render. -> useCallback Memorizes a function reference between renders. When you pass a callback to a child component, useCallback prevents the child from re-rendering because it receives a new function reference every render. -> useReducer Complex state logic that involves multiple sub-values or state transitions that depend on the previous state. Think of it as a mini Redux built directly into React. More predictable than multiple useState calls for complex state. These seven hooks cover state management, side effects, performance optimization, and DOM interaction. Everything else in React builds on these foundations. Which of these seven do you use least and think you should probably use more? #React #ReactHooks #FrontendDevelopment #JavaScript #WebDevelopment #Developers
To view or add a comment, sign in
-
-
⚛️ Understanding the Order of Hooks in React One important rule while working with React Hooks is that the order of hooks must remain the same on every render. React relies on the call order of hooks to correctly associate state and effects with a component. Example: function Example() { const [count, setCount] = useState(0); const [name, setName] = useState(""); useEffect(() => { console.log("Component mounted or updated"); }, []); return <div>{count}</div>; } 🔹 Hooks are executed top to bottom on every render. 🔹 Changing their order can break React’s internal state tracking. ❌ Incorrect usage: if (condition) { const [count, setCount] = useState(0); // ❌ Hooks inside condition } ✅ Correct approach: const [count, setCount] = useState(0); if (condition) { // logic here } 📌 Rules of Hooks 1️⃣ Call hooks only at the top level 2️⃣ Call hooks only inside React functions Understanding this helps avoid bugs and keeps components predictable. #React #ReactJS #FrontendDevelopment #WebDevelopment #JavaScript #Hooks
To view or add a comment, sign in
-
Most React developers use hooks every day. Few understand what's actually happening underneath. Here's what changed how I think about React 👇 🔗 Hook state isn't stored in your component. It lives on the Fiber node — in a linked list, ordered by position. That's the only reason the rules of hooks exist. Not arbitrary. Not convention. Pure implementation detail. ⚡ Three setCount calls don't always give you +3. If all three read the same stale closure value, you get +1. Functional updates are not a style preference. They're a correctness guarantee. 👻 Stale closures are silent killers. An interval that always logs 0 while your counter shows 20. No error. No warning. Just wrong behaviour — forever. useRef is the fix most engineers don't reach for. 🔇 useRef is not just for DOM refs. It's for any mutable value that needs to persist across renders without triggering one. Render counters. Previous values. Timer IDs. Stale closure fixes. All useRef. 🧹 Missing cleanup is a memory leak waiting to happen. Every interval, socket, and subscription needs a return function. No exceptions. The deeper you understand how hooks work, the fewer bugs you write. Sharing one deep dive at a time. 🚀 #React #useState #useEffect #useRef #FrontendEngineering #JavaScript #WebPerformance
To view or add a comment, sign in
-
🚀 Day 12/30 – Conditional Rendering in React Building dynamic UIs based on conditions ⚡ Today’s focus was on Conditional Rendering — a core concept that makes React apps interactive and user-driven. 🔍 What I learned: ✅ Conditional Rendering allows UI to change based on state ✅ React relies on pure JavaScript logic (if, ternary, &&) inside JSX ✅ Essential for real-world features like authentication, loaders, and error handling 💻 Example: function App() { const isLoggedIn = true; return ( <> {isLoggedIn ? <h1>Welcome User</h1> : <h1>Please Login</h1>} </> ); } 🔥 Common Patterns: 1️⃣ Ternary Operator (Most Used) {condition ? <A /> : <B />} 2️⃣ Logical AND (&&) {isLoggedIn && <Dashboard />} 3️⃣ If Statement (Outside JSX) if (!user) return <Login />; 💡 Real-World Use Cases: ✔️ Showing a loader while fetching data ✔️ Displaying error messages ✔️ Rendering dashboard after login ⚡ Advanced Insight: React doesn’t introduce new syntax for conditions — it simply leverages JavaScript inside JSX, making it flexible and powerful. 🔑 Key Takeaway: 👉 Conditional Rendering = Controlling what users see based on application state. 💬 Curious — which approach do you use more: ternary or if/else logic? #React #FrontendDevelopment #JavaScript #WebDevelopment #LearningInPublic
To view or add a comment, sign in
-
-
Why is your useEffect sitting idle even after you've updated the variable? Look at this snippet. If you’re coming from a Vanilla JS background, count = count + 1 looks perfectly fine. But in React? This code is technically "dead." Why this won't work: * Mutation != Re-render: You are updating a local let variable. React has no "spy" watching your local variables. Unless you call a state setter (like setCount), React doesn't know it needs to re-render the component. * The Lifecycle Gap: useEffect only checks its dependency array ([count]) when the component re-renders. Since the button click doesn't trigger a render, React thinks nothing has changed. * The "Reset" Trap: Even if the component did re-render for some other reason, the first line let count = 0; would run again, resetting your value back to zero every single time. The Fix? Stop treating React like a standard script. Use useState. When you use const [count, setCount] = useState(0), you’re telling React: "Hey, keep an eye on this value. If I change it via setCount, please refresh the UI and check my Effects." Pro-tip: In React, if you want the UI to "react," you have to use the Hook. Simple as that. #ReactJS #WebDevelopment #Frontend #CodingTips #IndiaDevs #Javascript
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
-
5 custom hooks I copy to every React project. Not from a library. Not from a tutorial. From production. Here's my starter kit: 𝟏. 𝘂𝘀𝗲𝗗𝗲𝗯𝗼𝘂𝗻𝗰𝗲() Search inputs, resize handlers, API calls — without re-render storms. I've seen apps making 40+ API calls per keystroke. This hook kills that. 𝟐. 𝘂𝘀𝗲𝗣𝗿𝗲𝘃𝗶𝗼𝘂𝘀() Tracks the previous value of any state or prop. Essential for debugging re-renders and building "changed from X to Y" logic. 𝟑. 𝘂𝘀𝗲𝗟𝗼𝗰𝗮𝗹𝗦𝘁𝗼𝗿𝗮𝗴𝗲() useState, but persistent. Survives page refresh. The catch most people miss: SSR safety. window doesn't exist on the server. 𝟒. 𝘂𝘀𝗲𝗠𝗲𝗱𝗶𝗮𝗤𝘂𝗲𝗿𝘆() Responsive logic in JS without CSS hacks. Show/hide components, change layouts, adjust behavior — all reactive, all clean. 𝟓. 𝘂𝘀𝗲𝗔𝗯𝗼𝗿𝘁𝗖𝗼𝗻𝘁𝗿𝗼𝗹𝗹𝗲𝗿() Auto-cancels fetch requests on component unmount. Nobody writes this hook. Until production breaks with a race condition at 2 AM. The pattern I've noticed after 8+ years: Senior engineers don't write more code. They carry better defaults. These 5 hooks are ~150 lines total. They prevent thousands of lines of bug fixes. What's in your starter kit? #React #JavaScript #TypeScript #CustomHooks #WebDevelopment #FrontendDevelopment #ReactHooks #CleanCode #SoftwareArchitecture #CodingTips
To view or add a comment, sign in
-
-
Important Frontend Concepts Checklist- 1. Pagination 2. Infinite Scrollbar 3. Debouncing 4. Websocket 5. REST vs GraphQl APIs 6. Local Storage vs Cookies 7. Authentication vs Authorization 8. Redux 9. Lazy Loading 10. Code Splitting 11. Bundle Size Optimization 12. Tree Shaking 13. Memoization (useMemo, useCallback) 14. Caching (Client + Server) 15. CSR vs SSR vs SSG vs ISR 16. Core Web Vitals (LCP, INP, CLS) 17. Cross Browser Compatibility 18. Optimistic UI Updates 19. Suspense (React) 20. Image Optimization (WebP, AVIF) 21. Accessibility (a11y) 22. Webpack 23. Micro-frontend Architecture 24. Testing- RTL, Jest, Playwright 25. Polyfills, Babel Get ebook with (detailed 232 ques = 50+ Reactjs Frequent Ques & Answers, 45+ Reactjs scenario based questions, 90+ frequently asked interview questions and answers, 50+ Output based ques & ans, 25+ Coding Questions & ans) 𝐄𝐛𝐨𝐨𝐤 𝐋𝐢𝐧𝐤: https://lnkd.in/gJMmH-PF Interview Guidance: https://lnkd.in/gr9PCuxd Follow on Instagram : https://lnkd.in/g-iSTsRd #javascript #javascriptdeveloper #reactjs #reactnative #vuejsdeveloper #angular #angulardeveloper
To view or add a comment, sign in
-
Important Frontend Concepts Checklist- 1. Pagination 2. Infinite Scrollbar 3. Debouncing 4. Websocket 5. REST vs GraphQl APIs 6. Local Storage vs Cookies 7. Authentication vs Authorization 8. Redux 9. Lazy Loading 10. Code Splitting 11. Bundle Size Optimization 12. Tree Shaking 13. Memoization (useMemo, useCallback) 14. Caching (Client + Server) 15. CSR vs SSR vs SSG vs ISR 16. Core Web Vitals (LCP, INP, CLS) 17. Cross Browser Compatibility 18. Optimistic UI Updates 19. Suspense (React) 20. Image Optimization (WebP, AVIF) 21. Accessibility (a11y) 22. Webpack 23. Micro-frontend Architecture 24. Testing- RTL, Jest, Playwright 25. Polyfills, Babel Get ebook with (detailed 232 ques = 50+ Reactjs Frequent Ques & Answers, 45+ Reactjs scenario based questions, 90+ frequently asked interview questions and answers, 50+ Output based ques & ans, 25+ Coding Questions & ans) 𝐄𝐛𝐨𝐨𝐤 𝐋𝐢𝐧𝐤: https://lnkd.in/gJMmH-PF Interview Guidance: https://lnkd.in/gr9PCuxd Follow on Instagram : https://lnkd.in/g-iSTsRd #javascript #javascriptdeveloper #reactjs #reactnative #vuejsdeveloper #angular #angulardeveloper
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