𝐀 𝐑𝐞𝐚𝐥 “𝐀𝐡𝐚” 𝐌𝐨𝐦𝐞𝐧𝐭 𝐢𝐧 𝐍𝐞𝐱𝐭.𝐣𝐬 Today I learned something very important while building my Blog Platform with Next.js Server Actions and it completely changed how I think about “data”. 𝑾𝒉𝒂𝒕 𝑰 𝒘𝒂𝒔 𝒅𝒐𝒊𝒏𝒈: I implemented an Admin Create Blog feature using: • Server Actions • useActionState • Cache revalidation with revalidatePath() The UI showed “Blog created successfully”, but the new blog was not actually persisting. At first glance, it felt confusing. 𝑻𝒉𝒆 𝑲𝒆𝒚 𝑹𝒆𝒂𝒍𝒊𝒛𝒂𝒕𝒊𝒐𝒏 (𝑽𝒆𝒓𝒚 𝑰𝒎𝒑𝒐𝒓𝒕𝒂𝒏𝒕) 𝘔𝘶𝘵𝘢𝘵𝘪𝘯𝘨 𝘢 𝘑𝘢𝘷𝘢𝘚𝘤𝘳𝘪𝘱𝘵 𝘢𝘳𝘳𝘢𝘺 𝘪𝘴 𝘕𝘖𝘛 𝘵𝘩𝘦 𝘴𝘢𝘮𝘦 𝘢𝘴 𝘴𝘢𝘷𝘪𝘯𝘨 𝘥𝘢𝘵𝘢. I was storing blogs in a JS file (blogs.js) and using blogs.push(). That works only in memory, not persistently. In modern Next.js: • Server Actions run in isolated contexts • In-memory data can reset on reload or re-render • Real persistence requires file system or a database 𝑾𝒉𝒂𝒕 𝑰 𝒕𝒓𝒖𝒍𝒚 𝒍𝒆𝒂𝒓𝒏𝒆𝒅 𝒕𝒐𝒅𝒂𝒚: ✅ Difference between mutation vs persistence ✅ Why real apps need databases or file-based storage ✅ How Server Actions handle mutations correctly ✅ Why cache revalidation alone is not enough ✅ How Next.js enforces production-grade architecture This was not a “bug”, it was a conceptual gap and fixing that gap matters more than fixing syntax. 𝑩𝒊𝒈𝒈𝒆𝒔𝒕 𝒕𝒂𝒌𝒆𝒂𝒘𝒂𝒚: If data matters, it must live outside memory. Frameworks don’t hide this, they teach it. (Day 17 of my Next.js learning series) #Nextjs #Reactjs #JavaScript #FrontendDevelopment #WebDevelopment
More Relevant Posts
-
Why useEffect Is Used Less in Modern React (and What Replaces It) useEffect is not deprecated. It’s not “bad”. But in modern React, we’ve learned to use it more intentionally. Earlier, useEffect was often used for: 1. Fetching data 2. Syncing one state with another 3. Running logic after every render Over time, this caused problems. The core issue with useEffect useEffect runs after render and is meant for side effects — things that happen outside React. When we use it to: 1. Manage data 2. Derive state 3. Control UI logic we introduce: 1. Extra renders 2. Complex dependency arrays 3. Hard-to-debug behavior In many cases, the effect exists only to “fix” state that could have been correct during render. What modern React encourages instead 1. Derive state during render If a value can be calculated from props or state, compute it directly instead of syncing it with an effect. This keeps components predictable and easier to reason about. 2. Use dedicated data-fetching tools (like TanStack Query) Data fetching is not just a side effect — it involves caching, retries, refetching, and background updates. Libraries like TanStack Query handle this declaratively, without manual effects or state management. 3. Server Components & Server Actions In frameworks like Next.js, data can be fetched on the server and sent ready to render. This removes the need for client-side effects for initial data loading and improves performance. 4. Suspense for async UI Suspense allows React to manage loading states at the rendering level instead of inside effects. When useEffect is still the right choice useEffect is still essential when interacting with things outside React: 1. Browser APIs 2. Subscriptions (WebSocket, events) 3. Timers 4. Analytics That’s exactly what it was designed for. Key takeaway Modern React doesn’t remove useEffect. "It reduces misuse by providing better, more specialized tools. If something can be derived, fetches data, or belongs on the server — useEffect is probably not the best tool." #ReactJS #FrontendDevelopment #ModernReact #JavaScript #TanStackQuery #WebDevelopment #ReactBestPractices #SoftwareEngineering
To view or add a comment, sign in
-
-
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
-
-
⚡ 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
-
-
Have you ever ended passing request, tenant or session session information around in your function call chain and asked yourself how could you make this look cleaner? Well there is a solution in Node.js! Welcome to AsyncLocalStorage (ALS)! Many languages and runtimes provide thread-local storage (TLS). In thread-per-request servers, TLS can be used to store request-scoped context and access it anywhere in the call stack. In Node.js we have something similar, although we don't use threads to process the requests, we use async contexts. Think of ALS as “thread-local storage” for async code: it lets you attach a small context object to the current async execution chain (Promises, async/await, timers, etc.) and read it anywhere downstream without having to pass that context data around on every function call, effectively making the function/method signature leaner. What it’s great for 🔎 Log correlation (requestId in every log line) 📈 Tracing/observability (span ids, metadata) 🧩 Request-scoped context (tenant/user, feature flags) 🧪 Diagnostics (debugging async flows) But with great power comes great responsibility, (sorry for the joke). A misused ALS can cause context leak to other requests and if not carefully designed you can start losing control of where things are set and where things are read. To solve this I like to treat ALS similar to a "Redux Store Slice", so each piece of related data I need to store in the ALS is a Slice. So I have slices for: auth, DB connections, soft delete behaviors, request logging, etc. And those slices are only set at the middleware level (or in Guards/Interceptors/Pipes if you use NestJS). Have you used ALS in production? What was your main win (or gotcha)? #nodejs #javascript #backend #nestjs #distributedtracing #cleanarchitecture
To view or add a comment, sign in
-
𝐈𝐬 𝐲𝐨𝐮𝐫 𝐮𝐬𝐞𝐄𝐟𝐟𝐞𝐜𝐭 𝐝𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐚𝐫𝐫𝐚𝐲 𝐬𝐞𝐜𝐫𝐞𝐭𝐥𝐲 𝐜𝐚𝐮𝐬𝐢𝐧𝐠 𝐢𝐧𝐟𝐢𝐧𝐢𝐭𝐞 𝐫𝐞-𝐫𝐞𝐧𝐝𝐞𝐫𝐬? 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
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
-
-
Performance vs. Readability: at the end of the day, both are O(n). While .reduce() avoids intermediate array allocations, modern JIT engines like V8 optimize these patterns so heavily that the overhead is often negligible for most use cases. In 4 years of building critical digital solutions, I’ve learned that "clever" code usually costs more in developer maintenance and debugging than it saves in micro-seconds of execution. Unless you’re handling massive datasets where memory pressure is a verified bottleneck, prioritize the human reader. Premature optimization is the root of unnecessary complexity. Always profile your system before sacrificing legibility.
⚡ 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
-
-
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
-
-
React Just Got Way Cleaner If you’ve worked with React for a while, you know how messy data fetching used to feel. useEffect + useState, extra boilerplate, and manual loading/error handling — not fun. This new approach feels refreshingly simple 🔥 React Data Fetching: Then vs Now Before: - useState - useEffect - manual loading & error handling Now: -use(fetchUser()) — that’s it. Example : function User() { const [user, setUser] = useState(null); fetchUser().then(setUser); if (!user) return <p>Loading...</p>; return <div>{user.name}</div>; } TO function User() { const user = use(fetchUser()); return <div>{user.name}</div>; } Declarative, readable, and much easier to reason about. React’s evolution is clearly leaning toward better developer experience, and I’m loving it. Cleaner code, fewer bugs, faster development 💯 What do you think — is this the future of data fetching in React? Drop your thoughts below and let’s learn from each other 👇 #ReactJS #JavaScript #Frontend #WebDevelopment #CleanCode #DeveloperExperience #ReactTips
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
-
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
insightful