👀If a useEffect only exists to sync derived data… it might not need to exist at all. Derived values don’t need to be stored. They can be computed. Fewer effects. Cleaner components ✨ #react #frontend #javascript #webdev #softwareengineering
Sergei Diachenko’s Post
More Relevant Posts
-
⚡ JavaScript Concept: Promises vs. Async/Await Stop the callback confusion! Choose the right tool for your async code. 🚀 🟢 Promises → The Foundation Action: Handles async operations via .then() and .catch(). Best for: Simple API fetches and parallel tasks. 🔴 Async/Await → The Standard Action: Cleaner syntax that reads like synchronous code. Best for: Complex logic and sequential API calls. #javascript #frontenddevelopment #reactjs #webperformance #webdevelopment
To view or add a comment, sign in
-
-
💡 JavaScript Tip: Async vs Await Handling asynchronous operations in JavaScript becomes much cleaner with async and await. 🔹 async makes a function return a Promise. 🔹 await pauses the execution until the Promise resolves. Together, they help write asynchronous code that looks simple, readable, and easier to maintain. Mastering this concept is essential when working with APIs, data fetching, and modern web applications. #JavaScript #AsyncAwait #WebDevelopment #CodingTips #FrontendDeveloper
To view or add a comment, sign in
-
-
⏳ “JavaScript is single-threaded.” I used to hear this everywhere. But then I had one question: If JavaScript is single-threaded… How does async code work? That’s when I learned about the Event Loop. Here’s the simple idea 👇 🧠 JavaScript has: • Call Stack • Web APIs • Callback Queue • Event Loop When async code runs (like setTimeout or fetch): 1️⃣ It moves to Web APIs 2️⃣ Once completed, it goes to the Callback Queue 3️⃣ The Event Loop checks if the call stack is empty 4️⃣ Then pushes it back to execute That’s why: console.log(1) setTimeout(() => console.log(2), 0) console.log(3) Output is: 1 3 2 Understanding this made debugging async bugs much easier. Frameworks don’t hide this. They rely on it. #JavaScript #EventLoop #WebDevelopment #FrontendDeveloper #NodeJS #SheryiansCodingSchool
To view or add a comment, sign in
-
-
𝐀𝐫𝐞 𝐲𝐨𝐮 𝐛𝐚𝐭𝐭𝐥𝐢𝐧𝐠 𝐩𝐡𝐚𝐧𝐭𝐨𝐦 𝐛𝐮𝐠𝐬 𝐢𝐧 𝐲𝐨𝐮𝐫 𝐑𝐞𝐚𝐜𝐭 𝐜𝐨𝐦𝐩𝐨𝐧𝐞𝐧𝐭𝐬? 𝐘𝐨𝐮𝐫 𝐮𝐬𝐞𝐄𝐟𝐟𝐞𝐜𝐭 𝐦𝐢𝐠𝐡𝐭 𝐛𝐞 𝐬𝐢𝐥𝐞𝐧𝐭𝐥𝐲 𝐥𝐲𝐢𝐧𝐠 𝐭𝐨 𝐲𝐨𝐮. Often, we forget that `useEffect` captures variables from its surrounding scope. If those variables change but aren't in the dependency array, your effect might run with stale values from a previous render. This leads to subtle, hard-to-trace bugs where your logic seems correct but the data is wrong. Example: ```javascript function MyComponent({ userId }) { const [data, setData] = useState(null); // Problematic: Forgetting 'userId' in dependencies useEffect(() => { fetch(`/api/users/${userId}/data`).then(res => res.json()).then(setData); // userId is referenced, but not in dependency array }, []); // <-- STALE CLOSURE if userId changes! // Correct: userId is a dependency useEffect(() => { fetch(`/api/users/${userId}/data`).then(res => res.json()).then(setData); }, [userId]); // <-- Correctly re-runs when userId changes } ``` The linter usually warns you about this, but it's easy to dismiss or override. Understanding why it warns you about missing dependencies is crucial for stable and performant React applications. It’s not just about avoiding infinite loops; it's about guaranteeing your effects always operate with the latest state and props. What's your most common `useEffect` pitfall, or a bug that took you ages to trace back to a stale closure? #React #Frontend #TypeScript #JavaScript #WebDevelopment
To view or add a comment, sign in
-
🚀 JavaScript Timing Insights: Understanding setTimeout Delay ⏳ Did you know that the delay you pass to setTimeout is just a minimum wait time, not a guaranteed execution time? 🔍 The actual time before your callback runs = delay + current call stack time + microtask processing + browser overhead What does that mean? Even setTimeout(fn, 0) doesn’t run immediately — it waits for the current synchronous code (call stack) and all microtasks (like Promises) to finish first. Browser policies, inactive tabs, and internal overhead add more variability. So, setTimeout is more like “execute no sooner than” rather than an exact timer. 💡 This is crucial to understand when writing async JavaScript code, especially for performance tuning and timing-critical operations. The microtask queue always drains before the task queue—impacting when your timeout callback runs. Understanding this helps you write smoother, more predictable async code! 🔧✨ #JavaScript #WebDevelopment #AsyncProgramming #EventLoop #CodingTips #Frontend #DeveloperInsights
To view or add a comment, sign in
-
-
𝙩𝙮𝙥𝙚𝙤𝙛 𝙣𝙪𝙡𝙡 === '𝙤𝙗𝙟𝙚𝙘𝙩' 𝙞𝙨 𝙤𝙣𝙚 𝙤𝙛 𝙅𝙖𝙫𝙖𝙎𝙘𝙧𝙞𝙥𝙩'𝙨 𝙢𝙤𝙨𝙩 𝙛𝙖𝙢𝙤𝙪𝙨 𝙡𝙞𝙚𝙨. And it's not a bug that slipped through, it's a 30-year-old fossil that the web is too fragile to remove. 𝙃𝙚𝙧𝙚'𝙨 𝙩𝙝𝙚 𝙧𝙚𝙖𝙡 𝙨𝙩𝙤𝙧𝙮 👇 𝗜𝘁 𝘀𝘁𝗮𝗿𝘁𝘀 𝗶𝗻 𝟭𝟵𝟵𝟱. 𝘉𝘳𝘦𝘯𝘥𝘢𝘯 𝘌𝘪𝘤𝘩 built JavaScript in 10 days. The original engine stored every value as a 32-bit word, with the lower bits acting as a "𝘵𝘺𝘱𝘦 𝘵𝘢𝘨" to tell the engine what kind of value it was dealing with. The tag for objects? 000. 𝗡𝗼𝘄 𝗵𝗲𝗿𝗲'𝘀 𝘄𝗵𝗲𝗿𝗲 𝗶𝘁 𝗴𝗼𝗲𝘀 𝘄𝗿𝗼𝗻𝗴. 𝘯𝘶𝘭𝘭 was represented as a null pointer. literally all zeros in memory: 0x00000000. When typeof read the lower bits of null, it saw... 000. The same bits that meant "𝘰𝘣𝘫𝘦𝘤𝘵." So it returned '𝘰𝘣𝘫𝘦𝘤𝘵'. And nobody caught it before it shipped. 𝗧𝗵𝗲 𝗳𝗶𝘅 𝘄𝗮𝘀 𝗽𝗿𝗼𝗽𝗼𝘀𝗲𝗱. 𝗧𝗵𝗲𝗻 𝗿𝗲𝗷𝗲𝗰𝘁𝗲𝗱. In 2006, the ECMAScript committee tried to fix it. typeof null would finally return '𝘯𝘶𝘭𝘭'. Makes total sense. But by then, millions of websites had already shipped code that accidentally relied on 𝘵𝘺𝘱𝘦𝘰𝘧 𝘯𝘶𝘭𝘭 === '𝘰𝘣𝘫𝘦𝘤𝘵'. Changing it would've broken the web. So the bug stayed. 𝘉𝘳𝘦𝘯𝘥𝘢𝘯 𝘌𝘪𝘤𝘩 himself has called it a mistake. 𝗧𝗵𝗲 𝗱𝗲𝗲𝗽𝗲𝗿 𝘁𝗮𝗸𝗲𝗮𝘄𝗮𝘆: Every language has fossils like this. Decisions made under pressure, in 10 days, that the entire ecosystem quietly built on top of. The next time you write val !== null && typeof val === 'object', you're not just being defensive. You're working around a 30-year negotiation between perfection and backwards compatibility. That's not a JavaScript quirk. 𝗧𝗵𝗮𝘁'𝘀 𝗲𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴. 💬 𝘞𝘩𝘢𝘵'𝘴 𝘺𝘰𝘶𝘳 𝘧𝘢𝘷𝘰𝘶𝘳𝘪𝘵𝘦 𝘑𝘚 𝘲𝘶𝘪𝘳𝘬? 𝘋𝘳𝘰𝘱 𝘪𝘵 𝘣𝘦𝘭𝘰𝘸 #JavaScript #WebDevelopment #Programming #Frontend #TIL #SoftwareEngineering #React #TypeScript
To view or add a comment, sign in
-
-
🚀 React Tip: Stop Writing Long useEffect Fetch Logic Instead of writing complex useEffect + useState code for API calls, use SWR (Stale-While-Revalidate). It gives you: ⚡ Faster UI with caching 🔄 Automatic data revalidation 🧹 Cleaner & shorter code Less boilerplate. More performance. #React #WebDevelopment #Frontend #JavaScript #CodingTips
To view or add a comment, sign in
-
-
𝗪𝗵𝗮𝘁 𝗶𝘀 𝘁𝗵𝗲 𝗘𝘃𝗲𝗻𝘁 𝗟𝗼𝗼𝗽 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁? JavaScript is single-threaded, but it can handle asynchronous operations efficiently using the Event Loop. The Event Loop is a mechanism that allows JavaScript to perform non-blocking operations like API calls, timers, and file reading while still running on a single thread. Here’s how it works: 1️⃣ Call Stack – Executes synchronous JavaScript code. 2️⃣ Web APIs – Handles async operations like "setTimeout", "fetch", and DOM events. 3️⃣ Callback Queue / Microtask Queue – Stores callbacks waiting to be executed. 4️⃣ Event Loop – Continuously checks if the call stack is empty and pushes queued callbacks to the stack for execution. This architecture allows JavaScript to manage asynchronous tasks without blocking the main thread, making it ideal for building fast and scalable web applications. Understanding the Event Loop is essential for mastering Promises, async/await, callbacks, and performance optimization in JavaScript. #JavaScript #EventLoop #WebDevelopment #FrontendDevelopment #NodeJS #AsyncJavaScript #CodingInterview #SoftwareEngineering #FullStackDeveloper #LearnToCode
To view or add a comment, sign in
-
⚡ JavaScript Tip: Cleaner Promise Error Handling Developers often use this trick to catch sync errors in promise chains. ❌ Before Promise.resolve() .then(() => JSON.parse(data)) .then(result => console.log(result)) .catch(err => console.error(err)); ✅ Now with Promise.try() Promise.try(() => JSON.parse(data)) .then(result => console.log(result)) .catch(err => console.error(err)); ✔ Less boilerplate ✔ More readable async flows ✔ Cleaner error handling Sometimes the best language improvements are the smallest ones. Would you start using Promise.try() in your projects? #JavaScript #ESNext #NodeJS #WebDevelopment #CodingTips
To view or add a comment, sign in
-
-
⚠️ One tricky thing in React that confused me at first: useEffect dependencies At first, I thought useEffect just runs magically. But the truth is very simple 👇 🧠 Think of useEffect like this: “React, run this code after render, and re‑run it only when I tell you to.” That “telling” happens through the dependency array. ✅ Examples (super simple) useEffect(() => { console.log("Runs once"); }, []); ➡️ Runs only when component mounts but for the below case useEffect(() => { console.log("Runs when count changes"); }, [count]); ➡️ Runs every time count changes 🚨 The tricky part If you use a variable inside useEffect but forget to add it as a dependency, React will use an old value (stale data). 🧠 Rule to remember: If you use it inside useEffect, it should be in the dependency array. Once I understood this, useEffect stopped feeling scary 😊 Just logic + clarity. #React #JavaScript #Hooks #useEffect #Frontend #LearningInPublic
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