𝐈𝐬 𝐲𝐨𝐮𝐫 𝐮𝐬𝐞𝐄𝐟𝐟𝐞𝐜𝐭 𝐝𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐚𝐫𝐫𝐚𝐲 𝐬𝐞𝐜𝐫𝐞𝐭𝐥𝐲 𝐜𝐚𝐮𝐬𝐢𝐧𝐠 𝐢𝐧𝐟𝐢𝐧𝐢𝐭𝐞 𝐫𝐞-𝐫𝐞𝐧𝐝𝐞𝐫𝐬? This is a common gotcha: if you're putting an object or an array directly into your useEffect dependency array, you might be triggering unnecessary re-runs on every render. Why? JavaScript's referential equality means { prop: 'value' } === { prop: 'value' } is false. A new object/array is created on every render, so useEffect sees a "change" even if the data inside is the same. The Pitfall: ```typescript function MyComponent({ data }) { // `config` is recreated on every render const config = { itemId: data.id, version: 1 }; useEffect(() => { // This effect runs on every render! // Even if data.id doesn't change. api.fetchDetails(config); }, [config]); // 🚨 Problem here! } ``` The Fix: Use useMemo for referential stability. ```typescript import React, { useEffect, useMemo } from 'react'; function MyComponent({ data }) { // `config` is now memoized and only changes when `data.id` changes const memoizedConfig = useMemo(() => ({ itemId: data.id, version: 1 }), [data.id]); // Dependencies for useMemo itself useEffect(() => { // This effect now only runs when memoizedConfig (and thus data.id) actually changes api.fetchDetails(memoizedConfig); }, [memoizedConfig]); // ✅ Correct! } ``` This simple change ensures your useEffect runs only when truly necessary, saving you from performance headaches and unexpected behavior. How do you tackle complex object or array dependencies in your React useEffect hooks? Share your go-to strategies! #ReactJS #FrontendDevelopment #JavaScript #WebDev #Performance
Prevent Unnecessary React useEffect Reruns with Memoized Dependencies
More Relevant Posts
-
Rethinking .filter().map() in Performance-Critical JavaScript Code As front-end developers, we often write code like this without thinking twice 👇 data .filter(item => item.isActive) .map(item => item.value) It’s clean. It’s readable. But in performance-sensitive or large-scale applications, it’s not always optimal. Why? 🤔 Because .filter() and .map() each create a new array, meaning: • Multiple iterations over the same data • Extra memory allocations • Unnecessary work in hot paths A better alternative in many cases 👇 data.reduce((acc, item) => { if (item.isActive) acc.push(item.value) return acc }, []) ✅ Single iteration ✅ Less memory overhead ✅ Better control over logic Does this mean you should never use .filter().map()? Of course not. 👉 Readability comes first for small datasets 👉 Optimization matters when dealing with large lists, frequent renders, or performance-critical code Key takeaway 🧠 Write clear code first. Optimize deliberately, not habitually. #JavaScript #ReactJS #FrontendDevelopment #WebPerformance #CleanCode #SoftwareEngineering
To view or add a comment, sign in
-
-
🧩 JavaScript – Arrays & Array Methods Working with Collections of Data In real-world applications, data rarely comes as a single value. JavaScript uses arrays to store and manage multiple values together. Mastering arrays is a must for frontend and full-stack development. 🔹 What is an Array? An array is a collection of values stored in a single variable. let skills = ["HTML", "CSS", "JavaScript"]; Each value has an index, starting from 0. skills[0]; // "HTML" 🔹 Basic Array Operations ➕ push() – Add to the end skills.push("React"); ➖ pop() – Remove from the end skills.pop(); ⬅ shift() – Remove from the start skills.shift(); ➡ unshift() – Add to the start skills.unshift("Git"); 👉 These methods help manage lists dynamically. 🔹 Looping Through Arrays forEach() – Perform an action for each item skills.forEach(skill => { console.log(skill); }); Used for: Displaying lists Logging data Simple iterations 🔹 Transforming Data with Array Methods 🔁 map() – Transform each element let lengths = skills.map(skill => skill.length); 👉 Returns a new array. 🔍 filter() – Select specific elements let longSkills = skills.filter(skill => skill.length > 3); 👉 Used for search, filtering, and conditions. 🧮 reduce() – Combine values into one let totalLength = skills.reduce( (sum, skill) => sum + skill.length, 0 ); 👉 Used for totals, calculations, and summaries. 🧠 Simple Way to Remember push / pop → add or remove items shift / unshift → work at the start map → transform filter → select reduce → combine ✅ Why Arrays Matter API data comes as arrays UI lists are built from arrays Frameworks depend heavily on array methods If you’re comfortable with arrays, JavaScript starts feeling powerful. . . #JavaScript #WebDevelopment #Arrays #ProgrammingBasics #LearningInPublic #FrontendDevelopment #FullStackJourney
To view or add a comment, sign in
-
-
🔁 JavaScript – Loops & Iterations Repeating Tasks Efficiently In real applications, we often need to perform the same task multiple times. Instead of writing the same code again and again, JavaScript gives us loops. Loops help write clean, efficient, and scalable code. 🔹 Why Loops Are Important Display lists of data Process user inputs Handle repetitive logic Improve performance and readability 🔹 for Loop Best when you know how many times to repeat. for (let i = 1; i <= 5; i++) { console.log(i); } Used for: Counters Fixed-length data UI rendering 🔹 while Loop Runs as long as a condition is true. let i = 1; while (i <= 3) { console.log(i); i++; } Used when: Number of iterations is unknown Condition controls execution 🔹 do…while Loop Runs at least once, even if the condition is false. let i = 5; do { console.log(i); i++; } while (i < 3); 👉 Useful when the task must execute once before checking the condition. 🔹 Looping Through Arrays Arrays often contain multiple values that need processing. let fruits = ["Apple", "Banana", "Mango"]; for (let i = 0; i < fruits.length; i++) { console.log(fruits[i]); } Used for: Displaying lists Processing data Applying logic to each item 🔹 break Statement Stops the loop immediately. for (let i = 1; i <= 5; i++) { if (i === 3) break; console.log(i); } 🔹 continue Statement Skips the current iteration and moves to the next one. for (let i = 1; i <= 5; i++) { if (i === 3) continue; console.log(i); } 🧠 Simple Way to Remember Loops → repeat work break → stop loop continue → skip one step Arrays + loops → real-world logic ✅ Key Takeaway If you understand loops well, you can: ✔ Handle data efficiently ✔ Write cleaner code ✔ Build real applications confidently Loops are a core skill every JavaScript developer must master. . . #JavaScript #WebDevelopment #ProgrammingBasics #LearningInPublic #FrontendDevelopment #FullStackJourney
To view or add a comment, sign in
-
-
🚀 CRUD System Project عملت مشروع CRUD System باستخدام HTML – CSS – JavaScript Features: ✔ Create / Update / Delete Products ✔ Search (Title & Category) ✔ Sort Data ✔ LocalStorage ✔ Light & Dark Mode 🔗 Demo / GitHub:https://lnkd.in/dATnWmZ8 #JavaScript #Frontend #CRUD #WebDevelopment
To view or add a comment, sign in
-
🚀 𝗙𝗶𝗿𝘀𝘁 - 𝗖𝗹𝗮𝘀𝘀 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮𝘀𝗰𝗿𝗶𝗽𝘁 In JavaScript, functions are first-class values. That means the language allows functions to be treated the same way as any other value. Practically, this gives us three abilities: • assign a function to a variable • pass a function as an argument • return a function from another function Example:- 1. when assigned as a value const sayHi = function () console.log("Hi"); } => 𝗧𝗵𝗶𝘀 𝗶𝘀 𝘄𝗵𝘆 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗲𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻𝘀, 𝗮𝗻𝗼𝗻𝘆𝗺𝗼𝘂𝘀 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀, 𝗮𝗻𝗱 𝗮𝗿𝗿𝗼𝘄 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗮𝗿𝗲 𝗻𝗼𝘁 𝘀𝗽𝗲𝗰𝗶𝗮𝗹 𝗰𝗮𝘀𝗲𝘀 , 𝘁𝗵𝗲𝘆’𝗿𝗲 𝗷𝘂𝘀𝘁 𝘃𝗮𝗹𝘂𝗲𝘀. 2. function passed as an argument function executeFunc (callback) { callback(); } executeFunc(sayHi) => 𝗧𝗵𝗶𝘀 𝗶𝘀 𝘁𝗵𝗲 𝗳𝗼𝘂𝗻𝗱𝗮𝘁𝗶𝗼𝗻 𝗳𝗼𝗿 𝗰𝗮𝗹𝗹𝗯𝗮𝗰𝗸𝘀, 𝗲𝘃𝗲𝗻𝘁 𝗵𝗮𝗻𝗱𝗹𝗲𝗿𝘀, 𝗮𝗻𝗱 𝗮𝘀𝘆𝗻𝗰 𝗰𝗼𝗱𝗲. 𝗧𝗵𝗲 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗶𝘀 𝗷𝘂𝘀𝘁 𝗯𝗲𝗶𝗻𝗴 𝗽𝗮𝘀𝘀𝗲𝗱 𝗹𝗶𝗸𝗲 𝗱𝗮𝘁𝗮. 3. Returning from another function function outer() { return function inner() { console.log("Inner function executed"); }; } const fn = outer(); fn(); => 𝗧𝗵𝗶𝘀 𝗯𝗲𝗵𝗮𝘃𝗶𝗼𝗿 𝗲𝗻𝗮𝗯𝗹𝗲𝘀 𝗰𝗹𝗼𝘀𝘂𝗿𝗲𝘀 𝗮𝗻𝗱 𝗵𝗶𝗴𝗵𝗲𝗿-𝗼𝗿𝗱𝗲𝗿 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 (𝗺𝗮𝗽, 𝗳𝗶𝗹𝘁𝗲𝗿, 𝗿𝗲𝗱𝘂𝗰𝗲). Most JavaScript patterns work because functions can be passed around like data. First-class functions are the rule that makes that possible. #Javascript #Functions #WebDevelopment #Frontend
To view or add a comment, sign in
-
-
⚡ Stop using .filter() and .map() together. Focus on iteration efficiency. I've seen this pattern in 100+ React codebases this year: const users = data .filter(user => user.active) .map(user => <UserCard {...user} />); Looks clean. Feels right. But here's the problem? Your code loops through that array TWICE. Your browser processes it twice. That's wasted computation. ━━━━━━━━━━━━━━━━━━━━━━━━━ The Issue: ❌ Two iterations over same array ❌ Creates intermediate arrays in memory ❌ Slows down large datasets ❌ Unnecessary extra passes ━━━━━━━━━━━━━━━━━━━━━━━━━ The Better Way: ✅ Use reduce() → Single loop ✅ One pass through data ✅ Better performance on large datasets ✅ Better edge case handling ━━━━━━━━━━━━━━━━━━━━━━━━━ Why This Matters: • Performance: Only loops once • Efficiency: Processes data in single iteration • Scalability: Handles 10K+ items smoothly • Optimization: No wasted computation Master the basics before reaching for optimization libraries. ━━━━━━━━━━━━━━━━━━━━━━━━━ 💬 Question for you: Do you prefer readability (.filter().map()) or performance (reduce())? Is there a middle ground I'm missing? Let's debate in the comments. 👇 #JavaScript #ReactJS #WebDevelopment #CodingTips #Performance #FrontendDevelopment #BestPractices #Coding #TechTips #DeveloperCommunity
To view or add a comment, sign in
-
-
React hooks, they're basically just JavaScript closures under the hood. Here's the simplest possible useState you could imagine: let value; function useState(initial) { value = value ?? initial; const setState = (newVal) => { value = newVal; // real React would re-render here }; return [value, setState]; } That’s it. The state lives in that outer variable, and the setter closes over it. But things get tricky with multiple renders. To avoid stale values, React stores hook data in an array and uses call order to match things up: let hooks = []; let index = 0; function useState(initial) { const i = index; // capture the current spot hooks[i] = hooks[i] ?? initial; const setState = (newVal) => { hooks[i] = newVal; // trigger re-render }; index++; return [hooks[i], setState]; } // reset index on each render function render() { index = 0; MyComponent(); } That little i = index capture is the closure magic that keeps everything in sync.
To view or add a comment, sign in
-
-
⚡ Stop using .filter() and .map() together. Your bundle size will thank you. I've seen this pattern in 100+ React codebases this year: const users = data .filter(user => user.active) .map(user => <UserCard {...user} />); Looks clean. Feels right. But here's the problem? Your code loops through that array TWICE. Your browser processes it twice. Your bundle gets heavier. ━━━━━━━━━━━━━━━━━━━━━━━━━ The Issue: ❌ Two iterations over same array ❌ Creates intermediate arrays in memory ❌ Slows down large datasets ❌ Unnecessary re-renders ━━━━━━━━━━━━━━━━━━━━━━━━━ The Better Way: ✅ Use reduce() → Single loop ✅ Cleaner code, fewer dependencies ✅ Smaller bundle size ✅ Better edge case handling ━━━━━━━━━━━━━━━━━━━━━━━━━ Why This Matters: • Performance: Only loops once • Dependencies: Zero extra libraries • Bundle Size: Noticeably smaller • Scalability: Handles 10K+ items smoothly Master the basics before reaching for optimization libraries. ━━━━━━━━━━━━━━━━━━━━━━━━━ 💬 Question for you: Do you prefer readability (.filter().map()) or performance (reduce())? Is there a middle ground I'm missing? Let's debate in the comments. 👇 #JavaScript #ReactJS #WebDevelopment #CodingTips #Performance #FrontendDevelopment #BestPractices #Coding #TechTips #DeveloperCommunity
To view or add a comment, sign in
-
-
Mastering JavaScript Arrays = Mastering Data. 📊🚀 Arrays are arguably the most used data structure in frontend development. Whether you are filtering users, transforming API data, or searching for specific items, you need these methods at your fingertips. The Array Toolkit: [cite_start]✅ Adding/Removing: push(), pop(), shift(), unshift() for stack/queue logic. ✅ Iteration: map(), filter(), reduce(), and forEach() for data transformation. ✅ Searching: find(), findIndex(), includes(), and at() to locate data instantly. ✅ Manipulation: splice(), slice(), concat(), and flat() to reshape structures. ✅ Sorting: sort() and reverse() to organize your lists. ✅ Validation: isArray(), every(), and some() to check data integrity. Swipe left to save this mega-reference! ⬅️ 💡 Found this helpful? * Follow M. WASEEM ♾️ for premium web development insights. 🚀 * Repost to help your network stay updated. 🔁 * Comment which array method saves you the most time! 👇 #javascript #webdevelopment #arrays #frontend #cheatsheet #codingtips #codewithalamin #webdeveloper #programming #js
To view or add a comment, sign in
-
✅ Understanding undefined vs null Clearly This morning’s code was: let x; console.log(x); console.log(typeof x); x = null; console.log(x); console.log(typeof x); 💡 Correct Output undefined undefined null object Yes , the last line surprises many 😄 Let’s understand why. 🧠 Simple Explanation : 🔹 Line 1: console.log(x); let x; x is declared But no value is assigned So JavaScript automatically gives it: undefined ✔ Output: undefined 🔹 Line 2: console.log(typeof x); The type of undefined is: "undefined" ✔ Output: undefined 🔹 Line 3: x = null; Here, we explicitly assign null. Important rule 👇 👉 null means intentional absence of value So: console.log(x); ✔ Output: null 🔹 Line 4: console.log(typeof x); This is the classic JavaScript quirk 👀 Even though x is null: typeof null returns: "object" ✔ Output: object 📌 This is a known bug in JavaScript kept for backward compatibility. 🎯 Key Takeaways : undefined → variable declared, value not assigned null → value explicitly set to “nothing” typeof undefined → "undefined" typeof null → "object" ❌ (historical JS bug) 📌 That’s why many developers check null like this: x === null 💬 Your Turn Did you already know typeof null is "object"? 😄 Comment “Knew it 👍” or “Learned today 😮” #JavaScript #LearnJS #FrontendDevelopment #CodingInterview #Basics #TechWithVeera #WebDevelopment
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