Aditi Kesharwani’s Post

💡 Today’s Learning: JavaScript & React Hooks Deep Dive 🚀 Continuing my journey of mastering JavaScript & React, I explored some powerful concepts that make our apps faster, cleaner, and more efficient. --- 🌐 JavaScript Concepts 🧭 𝐋𝐞𝐱𝐢𝐜𝐚𝐥 𝐄𝐧𝐯𝐢𝐫𝐨𝐧𝐦𝐞𝐧𝐭 & 𝐒𝐜𝐨𝐩𝐞 𝐂𝐡𝐚𝐢𝐧 When JavaScript executes code, it creates something called a Lexical Environment or Execution Context , which contains: 1️⃣ Environment Record – where variables and functions are stored. 2️⃣ Reference to the outer Lexical Environment – connecting it to its parent scope. Every function in JavaScript is linked to the Lexical Environment of 𝐭𝐡𝐞 𝐩𝐥𝐚𝐜𝐞 𝐰𝐡𝐞𝐫𝐞 𝐢𝐭 𝐰𝐚𝐬 𝐝𝐞𝐟𝐢𝐧𝐞𝐝, not where it’s called. This is what makes JavaScript lexically scoped (or statically scoped). 💡 When a variable is used, JavaScript looks for it in the current scope. If not found, it moves to the parent scope, and continues until it reaches the global scope. This chain of connected scopes is known as the Scope Chain. 🧩 Example: let a = 10; function outer() {  let b = 20;  function inner() {   let c = 30;   console.log(a + b + c); // 60  }  inner(); } outer(); 👉 Here, inner() can access a, b, and c because of the Scope Chain that connects its own Lexical Environment to the outer ones. --- ⚛ React Hooks 🧩𝐮𝐬𝐞𝐌𝐞𝐦𝐨 Used for memoization — helps skip expensive recalculations unless dependencies change. 𝐜𝐨𝐧𝐬𝐭 𝐦𝐞𝐦𝐨𝐕𝐚𝐥𝐮𝐞 = 𝐮𝐬𝐞𝐌𝐞𝐦𝐨(() => 𝐞𝐱𝐩𝐞𝐧𝐬𝐢𝐯𝐞𝐂𝐚𝐥𝐜𝐮𝐥𝐚𝐭𝐢𝐨𝐧(𝐚, 𝐛), [𝐚, 𝐛]); ✅ Returns a memoized value 💡 Used to optimize performance --- ⚙ 𝐮𝐬𝐞𝐂𝐚𝐥𝐥𝐛𝐚𝐜𝐤 Used to memoize functions and prevent unnecessary re-renders of child components. 𝐜𝐨𝐧𝐬𝐭 𝐦𝐞𝐦𝐨𝐢𝐳𝐞𝐝𝐅𝐧 = 𝐮𝐬𝐞𝐂𝐚𝐥𝐥𝐛𝐚𝐜𝐤(𝐂𝐚𝐥𝐥𝐛𝐚𝐜𝐤 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧), [𝐃𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐀𝐫𝐫𝐚𝐲]); ✅ Returns a memoized callback 💡 Perfect for passing functions as props 𝐃𝐢𝐟𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝐛𝐞𝐭𝐰𝐞𝐞𝐧 𝐮𝐬𝐞𝐌𝐞𝐦𝐨 𝐚𝐧𝐝 𝐮𝐬𝐞𝐂𝐚𝐥𝐥𝐛𝐚𝐜𝐤        useMemo            |        useCallback         Returns a memoized value    | Returns a memoized function Used for heavy computations | Used to prevent function re-renders --- 🌐 𝐮𝐬𝐞𝐂𝐨𝐧𝐭𝐞𝐱𝐭 When passing props becomes messy, Context API comes to the rescue. 1️⃣ Create Context    const UserContext = createContext(); 2️⃣ Wrap components inside a Provider    <UserContext.Provider value={user}>       <ChildA />    </UserContext.Provider> 3️⃣ Consume the context where needed    const user = useContext(UserContext); 💬 Helps avoid 𝐩𝐫𝐨𝐩 𝐝𝐫𝐢𝐥𝐥𝐢𝐧𝐠 and makes data sharing across components much cleaner. --- 💬 Each concept I explore — from Scope Chain in JavaScript to React Hooks — helps me understand how to write cleaner, faster, and more optimized code. #JavaScript #ReactJS #WebDevelopment #LearningJourney ---

I love the way you explain the Lexical Scoping

Like
Reply

To view or add a comment, sign in

Explore content categories