I stopped trusting the URL as the source of truth in my React app While building my latest expense tracker, I noticed a common pattern when working with dynamic routes. ➤ The Scenario: I’m using React Router for navigation and Context API for authentication. ⤷ My dashboard route was defined as: /dashboard/:username ➤ The Problem: Initially, I was reading the username directly from useParams(). It was only for UX purposes (I wanted to display the username in the URL). ⤷ But then I realized: → If a logged-in user manually changes the URL (e.g., /dashboard/ali), the UI state can become inconsistent with the authenticated user. → Even though the backend enforces authorization, the frontend should not treat the URL as the source of truth for user identity. ➤ The Fix: I made the Auth Context the single source of truth and added a guard to keep the UI in sync: ------------------------------------------------------- Dasboard.jsx useEffect(() => { if (user && username !== user) { navigate(`/dashboard/${user}`, { replace: true }); } }, [username, user, navigate]); ------------------------------------------------------- ➤ Key Takeaway: → The URL is user-controlled input → Auth state should always drive UI behavior → Keep your frontend consistent with the authentication layer Question I should ask experts: Is using URL params for display purposes (like usernames in routes) a good practice, or does it add unnecessary complexity in authenticated apps? #ReactJS #WebDevelopment #JavaScript #Frontend #SoftwareEngineering
Avoid Using URL as Source of Truth in React Apps
More Relevant Posts
-
🚀 Day 27/30 – React.memo (Stop Unnecessary Re-renders) Your app feels slow… but the issue might not be logic. 👉 It might be unnecessary re-renders 👀 Today I learned one of the most underrated React optimization tools ⚡ 👉 React.memo --- 💻 The Hidden Problem: You update one state in parent component… But React may also re-render child components ❌ Even when nothing changed. That means: ❌ Wasted renders ❌ Slower UI ❌ Poor performance in large apps --- 💻 The Solution: Use "React.memo" ✅ It tells React: 👉 “If props are same, skip re-render.” --- 💻 Example: const Child = React.memo(({ name }) => { console.log("Child Rendered"); return <h2>Hello {name}</h2>; }); function App() { const [count, setCount] = useState(0); return ( <> <button onClick={() => setCount(count + 1)}> {count} </button> <Child name="Umakant" /> </> ); } --- 🔥 What actually happens: 1️⃣ Count changes 2️⃣ Parent re-renders 3️⃣ Child gets same props 4️⃣ React skips Child render ⚡ --- 💡 Why this matters: ✅ Faster UI ✅ Better scalability ✅ Less wasted rendering work Especially useful in: - Dashboards - Large lists - Complex child components - Real production apps --- ⚡ Advanced Insight: "React.memo" uses shallow prop comparison 👀 So these can still re-render child: ❌ New object props ❌ New function props 👉 That’s why "useCallback" + "useMemo" are powerful partners. --- 🔥 Key Takeaway: Not every render is a problem… But unnecessary renders become expensive at scale. --- Be honest 👇 Have you ever optimized re-renders… or are you only styling components? 🚀 #React #ReactMemo #Performance #FrontendDevelopment #JavaScript
To view or add a comment, sign in
-
-
🗓️ Day 10 of 30 — Loading & Error UI in Next.js (loading.js / error.js) Most devs handle loading and errors as an afterthought. In Next.js App Router, they're first-class citizens. 🧠 ⏳ loading.js — Instant Loading States Next.js uses React Suspense under the hood. Just drop a loading.js file in any route folder and it automatically shows while the page fetches data. app/ dashboard/ loading.js ✅ shows while page.js loads page.js // app/dashboard/loading.js export default function Loading() { return <div className="skeleton">Loading dashboard...</div>; } No useState, no useEffect, no manual tracking. It just works. ✨ 💥 error.js — Graceful Error Handling Wrap any route with an error.js to catch errors without crashing the whole app. // app/dashboard/error.js "use client"; // must be a Client Component export default function Error({ error, reset }) { return ( <div> <h2>Something went wrong!</h2> <p>{error.message}</p> <button onClick={() => reset()}>Try again</button> </div> ); } The reset() function lets users retry without a full page reload. Super clean UX. 🔄 📁 How they scope Both files are layout-level — they only affect the segment they're placed in, not the whole app. app/ error.js → catches root-level errors dashboard/ loading.js → loading state for /dashboard only error.js → errors for /dashboard only page.js 💡 Key Takeaway File Purpose Component Type loading.js Auto suspense fallback Server or Client error.js Error boundary Client only Next.js makes production-quality UX the default — not the exception. What are you using for loading states in your projects? Skeletons, spinners, or something else? Drop it below 👇 #30DaysOfNextjs #Nextjs #React #WebDevelopment #LearningInPublic #Frontend #JavaScript #Programming
To view or add a comment, sign in
-
𝗘𝘃𝗲𝗿 𝘄𝗼𝗻𝗱𝗲𝗿𝗲𝗱 𝘄𝗵𝘆 𝘀𝗼𝗺𝗲 𝗮𝗽𝗽𝘀 𝗳𝗲𝗲𝗹 𝗶𝗻𝘀𝘁𝗮𝗻𝘁𝗹𝘆 𝘀𝗺𝗼𝗼𝘁𝗵 𝗮𝗻𝗱 𝗼𝘁𝗵𝗲𝗿𝘀 𝗳𝗲𝗲𝗹 𝗵𝗲𝗮𝘃𝘆? And most of the time, it is 𝗻𝗼𝘁 a 𝗵𝘂𝗴𝗲 𝗿𝗲𝘄𝗿𝗶𝘁𝗲. It is one 𝘀𝗶𝗺𝗽𝗹𝗲 𝗶𝗺𝗽𝗿𝗼𝘃𝗲𝗺𝗲𝗻𝘁 that 𝗿𝗲𝗺𝗼𝘃𝗲𝘀 𝗳𝗿𝗶𝗰𝘁𝗶𝗼𝗻. 𝗙𝗼𝗿 𝗲𝘅𝗮𝗺𝗽𝗹𝗲: A page was making the 𝘀𝗮𝗺𝗲 𝗔𝗣𝗜 𝗿𝗲𝗾𝘂𝗲𝘀𝘁 on 𝗲𝘃𝗲𝗿𝘆 𝗿𝗲𝗻𝗱𝗲𝗿. Nothing looked broken. But the 𝘀𝗰𝗿𝗲𝗲𝗻 𝗳𝗲𝗹𝘁 𝘀𝗹𝗼𝘄, 𝗵𝗲𝗮𝘃𝘆, and a little 𝘂𝗻𝘀𝘁𝗮𝗯𝗹𝗲. The 𝗳𝗶𝘅 was simple: 𝗖𝗮𝗰𝗵𝗲 𝘁𝗵𝗲 𝗿𝗲𝘀𝗽𝗼𝗻𝘀𝗲 and 𝘀𝘁𝗼𝗽 𝗳𝗲𝘁𝗰𝗵𝗶𝗻𝗴 the 𝘀𝗮𝗺𝗲 𝗱𝗮𝘁𝗮 𝗮𝗴𝗮𝗶𝗻 𝗮𝗻𝗱 𝗮𝗴𝗮𝗶𝗻. That 𝗼𝗻𝗲 𝗰𝗵𝗮𝗻𝗴𝗲 made the app 𝗳𝗲𝗲𝗹 𝗻𝗼𝘁𝗶𝗰𝗲𝗮𝗯𝗹𝘆 𝗯𝗲𝘁𝘁𝗲𝗿. 𝗪𝗵𝘆? Because users do not measure performance in milliseconds. They feel it as: • 𝗟𝗲𝘀𝘀 𝘄𝗮𝗶𝘁𝗶𝗻𝗴 • 𝗟𝗲𝘀𝘀 𝗳𝗹𝗶𝗰𝗸𝗲𝗿 • 𝗟𝗲𝘀𝘀 𝗹𝗼𝗮𝗱𝗶𝗻𝗴 • 𝗠𝗼𝗿𝗲 𝘁𝗿𝘂𝘀𝘁 That is the 𝗽𝗮𝗿𝘁 many 𝘁𝗲𝗮𝗺𝘀 𝗺𝗶𝘀𝘀. 𝗔 𝗽𝗿𝗼𝗱𝘂𝗰𝘁 𝗱𝗼𝗲𝘀 𝗻𝗼𝘁 𝗻𝗲𝗲𝗱 𝘁𝗼 𝗯𝗲 𝗽𝗲𝗿𝗳𝗲𝗰𝘁 𝘁𝗼 𝗳𝗲𝗲𝗹 𝗴𝗼𝗼𝗱. Sometimes it 𝗷𝘂𝘀𝘁 𝗻𝗲𝗲𝗱𝘀 to 𝘀𝘁𝗼𝗽 doing 𝘂𝗻𝗻𝗲𝗰𝗲𝘀𝘀𝗮𝗿𝘆 𝘄𝗼𝗿𝗸. A few examples of 𝘀𝗺𝗮𝗹𝗹 𝗰𝗵𝗮𝗻𝗴𝗲𝘀 that make a 𝗯𝗶𝗴 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲: 𝟭- 𝗖𝗮𝗰𝗵𝗲 𝗿𝗲𝗽𝗲𝗮𝘁𝗲𝗱 𝗱𝗮𝘁𝗮 Avoid calling the same endpoint when the data has not changed. 𝟮- 𝗢𝗽𝘁𝗶𝗺𝗶𝘇𝗲 𝗶𝗺𝗮𝗴𝗲𝘀 Large images often slow down the whole experience more than the code does. 𝟯- 𝗥𝗲𝗱𝘂𝗰𝗲 𝘂𝗻𝗻𝗲𝗰𝗲𝘀𝘀𝗮𝗿𝘆 𝗿𝗲-𝗿𝗲𝗻𝗱𝗲𝗿𝘀 Updating a component too often can make the whole UI feel sluggish. 𝟰- 𝗥𝗲𝗺𝗼𝘃𝗲 𝗲𝘅𝘁𝗿𝗮 𝗿𝗲𝗾𝘂𝗲𝘀𝘁𝘀 Every extra network call adds delay, especially on slower connections. The 𝗯𝗲𝘀𝘁 𝗽𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 wins are usually 𝗻𝗼𝘁 𝗱𝗿𝗮𝗺𝗮𝘁𝗶𝗰. 𝗧𝗵𝗲𝘆 𝗮𝗿𝗲 𝗾𝘂𝗶𝗲𝘁. 𝗕𝘂𝘁 𝘂𝘀𝗲𝗿𝘀 𝗳𝗲𝗲𝗹 𝘁𝗵𝗲𝗺 𝗶𝗺𝗺𝗲𝗱𝗶𝗮𝘁𝗲𝗹𝘆. And once an 𝗮𝗽𝗽 starts 𝗳𝗲𝗲𝗹𝗶𝗻𝗴 𝗳𝗮𝘀𝘁𝗲𝗿, it also starts 𝗳𝗲𝗲𝗹𝗶𝗻𝗴 𝗺𝗼𝗿𝗲 𝗿𝗲𝗹𝗶𝗮𝗯𝗹𝗲. That is a 𝗿𝗲𝗮𝗹 business 𝗮𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲. 𝗪𝗮𝗻𝘁 𝗮 𝗽𝗼𝘀𝘁 𝗼𝗻 𝗺𝘆 𝘁𝗼𝗽 𝘀𝗽𝗲𝗲𝗱 𝗼𝗽𝘁𝗶𝗺𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝗳𝗶𝘅𝗲𝘀 𝘁𝗵𝗮𝘁 𝗜 𝘂𝘀𝗲 𝗶𝗻 𝗿𝗲𝗮𝗹 𝗡𝗲𝘅𝘁.𝗷𝘀 𝗽𝗿𝗼𝗷𝗲𝗰𝘁𝘀? #Nextjs #ReactJS #WebPerformance #FrontendDevelopment #PerformanceOptimization #WebOptimization #CoreWebVitals #JavaScript #SoftwareEngineering #UXDesign #ProductEngineering #APIIntegration #Caching #WebDevelopment #TechTips
To view or add a comment, sign in
-
-
Most frontend apps don’t fail because of UI… They fail because of poor API handling. After working on production apps, here are API best practices I always follow 👇 --- 💡 1. Retry Mechanism (Don’t fail instantly) Temporary failures happen — retry smartly async function fetchWithRetry(url, retries = 3) { try { return await fetch(url); } catch (err) { if (retries === 0) throw err; return fetchWithRetry(url, retries - 1); } } --- 💡 2. Add Timeout (Avoid hanging requests) const controller = new AbortController(); setTimeout(() => controller.abort(), 5000); fetch(url, { signal: controller.signal }); --- 💡 3. Proper Error Handling if (!res.ok) { throw new Error(`Error: ${res.status}`); } --- 💡 4. Caching (Save API calls 🚀) - Use browser cache / memory - Tools like React Query / SWR --- 💡 5. Loading & Fallback UI Never leave users guessing… ✔ Show loaders ✔ Show fallback data --- 💡 6. Debounce API Calls Avoid spamming APIs (search inputs) --- 💡 7. Handle Edge Cases ✔ Network failure ✔ Empty response ✔ Partial data --- 🔥 Good API handling = better UX + better performance --- #JavaScript #Frontend #WebDevelopment #API #ReactJS #SoftwareEngineering #Coding
To view or add a comment, sign in
-
🚀 Enhancing Web App Performance with Easy Techniques When it comes to creating scalable web applications, optimizing performance is super important! Recently, I discovered three fantastic techniques that can really boost both performance and the user experience: 🔹 Debouncing Debouncing is a great way to delay API calls until the user has finished typing. 👉 For instance, in a search bar, rather than calling the API with every keystroke, we wait until the user completes their input. ✅ This reduces unnecessary API calls ✅ It makes everything run more efficiently 🔹 Lazy Loading Lazy loading means that components are only loaded when they’re actually needed. 👉 For example, in React, we can dynamically load pages or components as required. ✅ This cuts down the initial load time ✅ It enhances the speed of the application 🔹 Throttling Throttling is all about limiting how often a function can run within a certain timeframe. 👉 A great example is preventing multiple API calls when a user clicks a button repeatedly. ✅ This helps avoid server overload ✅ It boosts stability 💡 These tiny optimizations can really make a huge difference when building scalable, high-performance applications. #WebDevelopment #ReactJS #PerformanceOptimization #JavaScript #FullStackDeveloper #LearningInPublic
To view or add a comment, sign in
-
Your Next.js app throws a "Hydration failed" warning ⚠️. You ignore it because the UI eventually loads fine. This is a massive architectural mistake. It is silently destroying your page speed. 👇 The Trap: The React Panic 💥 Server-Side Rendering (SSR) is supposed to be fast. The server sends pre-built HTML, and React "hydrates" it by attaching event listeners. But React expects the Server HTML and the initial Client HTML to be 100% identical. What happens if you use window.innerWidth or new Date()? The server generates one value. The browser generates another. React sees the mismatch and panics. It throws away the perfectly good Server HTML. It completely rebuilds the entire DOM from scratch on the client. You just forced the browser to do twice the work. Your SSR was completely wasted. The Fix: Two-Pass Rendering 🧊 If a component relies on browser-only data, delay that specific render until after hydration. 1️⃣ State: const [isMounted, setIsMounted] = useState(false); 2️⃣ Hydrate: useEffect(() => setIsMounted(true), []); 3️⃣ Render: Only show the browser-specific UI if isMounted is true. Otherwise, render a skeleton. The Takeaway: Next.js gives you a massive performance head start. Don't ruin it by forcing React to throw away the server's hard work. Are you ignoring hydration warnings, or actively fixing your component boundaries? 👇 #NextJS #ReactJS #FrontendEngineering #WebDevelopment #SoftwareArchitecture #SystemDesign #PerformanceOptimization
To view or add a comment, sign in
-
-
⚡ 𝗬𝗼𝘂𝗿 𝗥𝗲𝗮𝗰𝘁 𝗮𝗽𝗽 𝗶𝘀 𝘀𝗹𝗼𝘄? It's probably your images. A recent deep dive shows you can cut LCP from ~8𝒔 → ~1𝒔 just by fixing image handling 👇 🧠 𝗠𝗼𝘀𝘁 𝗱𝗲𝘃𝘀 𝗼𝗽𝘁𝗶𝗺𝗶𝘇𝗲 𝗰𝗼𝗱𝗲... 𝗻𝗼𝘁 𝗮𝘀𝘀𝗲𝘁𝘀 👉 Images are often the 𝒉𝒆𝒂𝒗𝒊𝒆𝒔𝒕 𝒑𝒂𝒓𝒕 of your app 🚀 𝗧𝗵𝗲 6-𝘀𝘁𝗲𝗽 𝗽𝗹𝗮𝘆𝗯𝗼𝗼𝗸 1. Find bottleneck images (LCP first) 2. Compress aggressively (no visible quality loss) 3. Move images to a CDN (stop bundling them ❌) 4. Use modern formats (WebP / AVIF) 5. Serve responsive images (𝑠𝑟𝑐𝑠𝑒𝑡) 6. Lazy load everything below the fold ⚠️ 𝗧𝗵𝗲 𝗯𝗶𝗴𝗴𝗲𝘀𝘁 𝗺𝗶𝘀𝘁𝗮𝗸𝗲 Bundling images inside your React app. That means: * Bigger JS bundle * Slower load * Worse Core Web Vitals 📊 𝗧𝗵𝗲 𝗿𝗲𝘀𝘂𝗹𝘁? Massive improvements in: * LCP * UX * SEO 🧭 𝗧𝗵𝗲 𝗻𝗲𝘄 𝗿𝘂𝗹𝗲: 👉 Treat images like infrastructure, not assets 🚀 The easiest performance win: 𝑶𝒑𝒕𝒊𝒎𝒊𝒛𝒆 𝒚𝒐𝒖𝒓 𝒊𝒎𝒂𝒈𝒆𝒔 𝒃𝒆𝒇𝒐𝒓𝒆 𝒐𝒑𝒕𝒊𝒎𝒊𝒛𝒊𝒏𝒈 𝒚𝒐𝒖𝒓 𝒄𝒐𝒅𝒆. 🔗 Source: https://lnkd.in/db2mgwuD #React #WebPerformance #Frontend #JavaScript #WebDev #Performance
To view or add a comment, sign in
-
-
You click 'Like' on a post. The heart turns red instantly. ❤️ But did the server confirm it? Not yet. That's Optimistic Updates — and it's one of the smartest UX tricks in frontend development. --- Here's the idea: Instead of waiting for the server to respond, you update the UI immediately — then reconcile once the response arrives. ✅ Server succeeds → nothing changes, user never noticed the gap ❌ Server fails → you roll back silently --- Why does it matter? → Your app feels instant, even on a 3G connection → Users don't stare at loading spinners for simple actions → The difference between a "slow app" and a "fast app" is often just this one pattern --- But here's what we must get right: ✔ Always snapshot the previous state before updating the UI ✔ Roll back immediately if the server responds with an error ✔ Sync with the server after the request settles — success or failure Libraries like React Query make this clean with onMutate → onError → onSettled hooks. --- When TO use it: ✔ Likes, toggles, checkboxes ✔ Drag-and-drop reordering ✔ Read/unread status ✔ Auto-save form fields When NOT to: ✗ Payments ✗ Irreversible deletes ✗ Anything with real business consequences --- The rule of thumb: use optimistic updates when failure is rare and rollback is easy. Are you using this pattern in your apps? Drop a comment 👇 #React #FrontendDevelopment #WebDev #JavaScript #UX #SoftwareDevelopment
To view or add a comment, sign in
-
-
🚀 𝗗𝗲𝗰𝗼𝗱𝗲 𝗠𝗘𝗥𝗡 𝘄𝗶𝘁𝗵 𝗠𝗲 – 𝗗𝗮𝘆 𝟳 Today’s focus wasn’t just UI… It was about writing smarter React code. I explored 𝗖𝘂𝘀𝘁𝗼𝗺 𝗛𝗼𝗼𝗸𝘀— a concept that changes how you structure your applications. Instead of repeating logic in multiple components, I learned how to extract it into a reusable function. So I built a simple project to apply it 👇 📌 𝗣𝗿𝗼𝗷𝗲𝗰𝘁: Notes App with Auto Save A minimal app, but with a strong concept behind it. ✔ Notes are saved automatically ✔ Data persists even after refresh ✔ No backend used — everything handled in the browser ✔ Clean and simple UI with Tailwind CSS ⚙️ 𝗪𝗵𝗮𝘁 𝗺𝗮𝗱𝗲 𝗶𝘁 𝗶𝗻𝘁𝗲𝗿𝗲𝘀𝘁𝗶𝗻𝗴? I created a custom hook: useLocalStorage This hook handles: • State management • Data storage • Sync between UI and localStorage One hook → reusable logic → cleaner components 🧠 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆: When your logic is reusable, your code becomes scalable. And that’s where React starts to feel powerful. 🔗 𝗖𝗼𝗱𝗲: https://lnkd.in/gBuMk4TG Learning one concept at a time. Building one project at a time. #MERN #ReactJS #CustomHooks #WebDevelopment #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
-
Your React App is Slow… and You Might Be the Reason Most developers use useMemo and React.memo but very few actually understand when NOT to use them ⚠️ I recently debugged a performance issue in a React dashboard. Everything looked optimized — but still, the UI was lagging. The reason? 👉 Memoization was used… but used wrongly. ⚡ Let’s break it down simply: 🔹 useMemo — Caches Calculations Use it when you have expensive computations Example: const filteredData = useMemo(() => { return data.filter(item => item.active); }, [data]); ✔ Prevents unnecessary recalculations ❌ Useless for cheap operations 🔹 React.memo — Stops Re-renders Wrap your component to avoid re-render when props don’t change const Card = React.memo(({ item }) => { return <div>{item.name}</div>; }); ✔ Great for large lists / heavy UI ❌ Useless if props change every render 🔥 The Real Problem Most people do this: Wrap everything in React.memo Add useMemo everywhere Hope performance improves 👉 Result: More complexity, same performance (sometimes worse) 💡 What Actually Works ✔ Use useMemo only for heavy calculations ✔ Use React.memo only when re-renders are costly ✔ Avoid passing new object/array references every render ✔ Measure performance before optimizing 🧠 The Mindset Shift Optimization is not about adding hooks It’s about removing unnecessary work 🚀 Final Takeaway Don’t optimize blindly. First understand: Why is it re-rendering? Is it actually slow? Then use the right tool. 👇 Curious — have you ever faced unnecessary re-renders in your app? #ReactJS #FrontendDevelopment #JavaScript #WebPerformance #ReactHooks #SoftwareEngineering #CleanCode #DevTips
To view or add a comment, sign in
-
More from this author
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