šŸ“„Code Snippets

Browse 15 amazing code snippets from our community

Using Intersection Observer for Lazy Loading and Scroll-Based Animations

<p style="margin-left:0px;">The Intersection Observer API lets you efficiently detect when an element enters or leaves the viewport without costly scroll events. This is ideal for lazy‑loading images, triggering animations, or loading infinite‑scroll content. By observing a target element, you get a callback with its intersection ratio, letting you decide when to act. For lazy loading, swap a placeholder’s src with the real image once the element is about to enter the viewport. For animations, add a class that starts a CSS animation when the element becomes visible. The API works in all modern browsers and can be polyfilled for older ones. Use a rootMargin to start loading slightly before the element is fully visible, improving perceived performance. Remember to disconnect observers when components unmount to avoid memory leaks. This pattern reduces initial page weight, improves Core Web Vitals (especially LCP), and keeps interactions smooth without jank from scroll‑based throttling.</p><p><br>&nbsp;</p>

javascript
#frontend#intersection-observer#lazy-loading#performance#scroll-animations
šŸ‘¤manish
šŸ“…17 May 2026

Leveraging Astro Islands for Interactive Content-Heavy Sites

<p style="margin-left:0px;">Astro’s island architecture lets you build mostly static pages with isolated interactive components, ideal for marketing sites, blogs, or documentation where most content is static but you need a few dynamic widgets (like a newsletter form, comment section, or live chat). By default, Astro renders HTML with zero client‑side JavaScript. You then ā€œhydrateā€ only the components you mark as interactive, choosing a hydration strategy (idle, visible, media, or always). This reduces JavaScript payload, improves Time to Interactive, and keeps Core Web Vitals strong while still enabling rich interactivity where needed. For a fintech marketing page, you could keep product copy, testimonials, and FAQs static, then hydrate a rate‑calculator widget only when it enters the viewport. The pattern scales well: you can mix frameworks (React, Svelte, Vue) inside the same project, each island loading its own framework code only when used. Use Astro’s built‑in image optimization and scoped CSS to further cut payload.</p><p><br>&nbsp;</p>

html
#architecture#astro#frontend#islands#performance#ssr
šŸ‘¤manish
šŸ“…17 May 2026

Maintaining a Simple Global State Store in React Without Extra Libraries

<p style="margin-left:0px;">For many mid‑size frontends, you do not need Redux or heavy state libraries to manage global state. A simple combination of React Context and a single store hook is often enough for things like auth, theme, and user preferences. The idea is to keep most state local to components and lift only truly shared data into a lightweight global store. This keeps components easier to reason about, avoids prop‑drilling, and reduces unnecessary dependencies.</p><p style="margin-left:0px;">A practical pattern is: create a context, use a provider at the app root, and expose a custom hook like <code>useAppStore</code> for reading and updating shared state. Keep the store shape small and focused on stable, long‑lived values (e.g., <code>user</code>, <code>theme</code>, <code>isSidebarOpen</code>). For more dynamic data (like server lists), prefer data‑fetching hooks per screen. This approach scales well for typical dashboards, support tools, or e‑commerce consoles without over‑engineering.</p><p><br>&nbsp;</p>

javascript
#context#frontend#global-state#react#state-management
šŸ‘¤manish
šŸ“…17 May 2026

Implementing Optimistic UI Updates in React Applications for Better Perceived Performance

<p style="margin-left:0px;">Optimistic UI updates improve perceived performance by updating the interface immediately when a user action occurs, assuming the server request will succeed. This technique is especially valuable in e‑commerce (e.g., adding items to a cart) or financial apps (e.g., transferring funds) where waiting for server responses can feel sluggish. If the request fails, you roll back the UI and show an error. Implementing this pattern involves: 1) mutating local state optimistically, 2) sending the request to the server, 3) reverting on failure or confirming on success. Libraries like React Query or SWR simplify this with built‑in mutate and rollback mechanisms, but you can also implement it manually with useState and useEffect. Key considerations include handling network errors, preventing race conditions, and providing clear undo or error messages. This pattern keeps the UI responsive and gives users confidence that their actions are immediate, even if background processes take time.</p><p><br>&nbsp;</p>

javascript
#frontend#optimistic-ui#performance#react#user-experience
šŸ‘¤manish
šŸ“…17 May 2026