<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arjun Kumar</title>
    <description>The latest articles on DEV Community by Arjun Kumar (@arjun_computer_geek).</description>
    <link>https://dev.to/arjun_computer_geek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F786731%2Fb16a567f-1a17-4b25-a900-bf00a0af6ab2.png</url>
      <title>DEV Community: Arjun Kumar</title>
      <link>https://dev.to/arjun_computer_geek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arjun_computer_geek"/>
    <language>en</language>
    <item>
      <title>📬 SMTP Configuration Explained</title>
      <dc:creator>Arjun Kumar</dc:creator>
      <pubDate>Fri, 27 Feb 2026 06:09:54 +0000</pubDate>
      <link>https://dev.to/arjun_computer_geek/smtp-configuration-explained-4d86</link>
      <guid>https://dev.to/arjun_computer_geek/smtp-configuration-explained-4d86</guid>
      <description>&lt;p&gt;&lt;strong&gt;What to Use, When to Use It, and Why It Breaks at 2AM&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Email delivery looks simple from the outside. A button says “Send”. A message flies away. Magic. ✨&lt;/p&gt;

&lt;p&gt;Behind that button lives SMTP. A protocol older than most frontend frameworks and still more reliable than half of them.&lt;/p&gt;

&lt;p&gt;Let’s dissect it properly. Clean. Practical. No fluff.&lt;/p&gt;




&lt;h2&gt;
  
  
  📡 What Is SMTP?
&lt;/h2&gt;

&lt;p&gt;SMTP stands for Simple Mail Transfer Protocol.&lt;/p&gt;

&lt;p&gt;It is the protocol used to send emails between servers and from applications to mail servers.&lt;/p&gt;

&lt;p&gt;It does not handle inbox reading. That’s IMAP or POP3.&lt;br&gt;
SMTP is the delivery truck. 🚚&lt;/p&gt;


&lt;h2&gt;
  
  
  🧩 Core SMTP Configuration Fields
&lt;/h2&gt;

&lt;p&gt;When configuring SMTP in Node.js, NestJS, or any backend, you usually see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  host: "",
  port: 000,
  secure: false,
  auth: {
    user: "",
    pass: ""
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s decode each part.&lt;/p&gt;




&lt;p&gt;1️⃣ host&lt;/p&gt;

&lt;p&gt;The SMTP server address.&lt;/p&gt;

&lt;p&gt;Examples:&lt;br&gt;
-smtp.gmail.com&lt;br&gt;
-smtp.sendgrid.net&lt;br&gt;
-mail.yourdomain.com&lt;/p&gt;

&lt;p&gt;This is where your app connects to send mail.&lt;/p&gt;



&lt;p&gt;2️⃣ port&lt;/p&gt;

&lt;p&gt;The communication channel. Different ports = different security expectations.&lt;/p&gt;

&lt;p&gt;Here’s the real breakdown 👇&lt;/p&gt;

&lt;p&gt;Port    Usage   Secure Value&lt;/p&gt;

&lt;p&gt;465 SSL/TLS (immediate encryption)  true&lt;br&gt;
587 STARTTLS (recommended)  false&lt;br&gt;
2525    Alternative to 587  false&lt;br&gt;
25  Server to server (often blocked)    false&lt;/p&gt;


&lt;h3&gt;
  
  
  🔐 Port 465
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;port: 465,
secure: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Encryption starts immediately.&lt;/p&gt;

&lt;p&gt;Use when:&lt;/p&gt;

&lt;p&gt;Provider explicitly supports SSL on 465&lt;/p&gt;

&lt;p&gt;Corporate mail setups&lt;/p&gt;

&lt;p&gt;Traditional configurations&lt;/p&gt;



&lt;p&gt;🤝 Port 587 (Recommended)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;port: 587,
secure: false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Connection starts normal, then upgrades to TLS.&lt;/p&gt;

&lt;p&gt;Use when:&lt;/p&gt;

&lt;p&gt;Sending transactional emails&lt;/p&gt;

&lt;p&gt;Production apps&lt;/p&gt;

&lt;p&gt;Gmail, SendGrid, Mailgun setups&lt;/p&gt;

&lt;p&gt;This is the industry standard.&lt;/p&gt;




&lt;p&gt;🛟 Port 2525&lt;/p&gt;

&lt;p&gt;port: 2525,&lt;br&gt;
secure: false&lt;/p&gt;

&lt;p&gt;Used when:&lt;/p&gt;

&lt;p&gt;587 is blocked by firewall&lt;/p&gt;

&lt;p&gt;Cloud providers restrict port 25&lt;/p&gt;

&lt;p&gt;Hosting environments limit SMTP traffic&lt;/p&gt;

&lt;p&gt;Think of it as the reliable backup lane.&lt;/p&gt;




&lt;p&gt;⚠️ Port 25&lt;/p&gt;

&lt;p&gt;Old-school SMTP. Mostly used for server-to-server communication.&lt;/p&gt;

&lt;p&gt;Avoid for application-level sending unless specifically required.&lt;/p&gt;




&lt;p&gt;🔑 secure: true vs secure: false&lt;/p&gt;

&lt;p&gt;This setting controls how encryption is initiated.&lt;/p&gt;

&lt;p&gt;secure: true&lt;/p&gt;

&lt;p&gt;SSL from first byte&lt;/p&gt;

&lt;p&gt;Used with port 465&lt;/p&gt;

&lt;p&gt;secure: false&lt;/p&gt;

&lt;p&gt;Uses STARTTLS&lt;/p&gt;

&lt;p&gt;Encryption begins after connection&lt;/p&gt;

&lt;p&gt;Used with 587 or 2525&lt;/p&gt;

&lt;p&gt;Common mistake:&lt;/p&gt;

&lt;p&gt;port: 587,&lt;br&gt;
secure: true ❌&lt;/p&gt;

&lt;p&gt;That causes handshake failure.&lt;/p&gt;




&lt;p&gt;🔐 auth&lt;/p&gt;

&lt;p&gt;Authentication credentials.&lt;/p&gt;

&lt;p&gt;auth: {&lt;br&gt;
  user: process.env.SMTP_USER,&lt;br&gt;
  pass: process.env.SMTP_PASS&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Best practices:&lt;/p&gt;

&lt;p&gt;Never hardcode credentials&lt;/p&gt;

&lt;p&gt;Use environment variables&lt;/p&gt;

&lt;p&gt;Prefer app passwords for Gmail&lt;/p&gt;

&lt;p&gt;Use API-based mail providers in production&lt;/p&gt;




&lt;p&gt;🏗 Where to Use SMTP?&lt;/p&gt;

&lt;p&gt;1️⃣ Small Apps / MVPs&lt;/p&gt;

&lt;p&gt;Use:&lt;/p&gt;

&lt;p&gt;Gmail SMTP&lt;/p&gt;

&lt;p&gt;Port 587&lt;/p&gt;

&lt;p&gt;secure: false&lt;/p&gt;

&lt;p&gt;Good for:&lt;/p&gt;

&lt;p&gt;Internal tools&lt;/p&gt;

&lt;p&gt;Testing&lt;/p&gt;

&lt;p&gt;Early-stage projects&lt;/p&gt;




&lt;p&gt;2️⃣ Production SaaS / Enterprise Apps&lt;/p&gt;

&lt;p&gt;Use:&lt;/p&gt;

&lt;p&gt;SendGrid&lt;/p&gt;

&lt;p&gt;Mailgun&lt;/p&gt;

&lt;p&gt;Amazon SES&lt;/p&gt;

&lt;p&gt;Dedicated SMTP relay&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Better deliverability&lt;/p&gt;

&lt;p&gt;SPF, DKIM, DMARC support&lt;/p&gt;

&lt;p&gt;Rate limiting protection&lt;/p&gt;

&lt;p&gt;Analytics &amp;amp; bounce handling&lt;/p&gt;




&lt;p&gt;3️⃣ High-Scale Notification Systems&lt;/p&gt;

&lt;p&gt;If you’re building something like:&lt;/p&gt;

&lt;p&gt;OTP systems&lt;/p&gt;

&lt;p&gt;Approval workflows&lt;/p&gt;

&lt;p&gt;Finance notifications&lt;/p&gt;

&lt;p&gt;Campaign systems&lt;/p&gt;

&lt;p&gt;You should:&lt;/p&gt;

&lt;p&gt;Separate email service layer&lt;/p&gt;

&lt;p&gt;Use queues (BullMQ / RabbitMQ / NATS)&lt;/p&gt;

&lt;p&gt;Implement retry logic&lt;/p&gt;

&lt;p&gt;Track bounce &amp;amp; failure states&lt;/p&gt;

&lt;p&gt;SMTP alone is transport. Reliability comes from architecture.&lt;/p&gt;




&lt;p&gt;🧠 Production Best Practices&lt;/p&gt;

&lt;p&gt;✅ Always Enable TLS&lt;/p&gt;

&lt;p&gt;Even if secure: false, STARTTLS should be enabled.&lt;/p&gt;

&lt;p&gt;✅ Configure SPF, DKIM, DMARC&lt;/p&gt;

&lt;p&gt;Without these, emails land in spam.&lt;/p&gt;

&lt;p&gt;✅ Use a Dedicated Domain&lt;/p&gt;

&lt;p&gt;Don’t send from personal Gmail in production.&lt;/p&gt;

&lt;p&gt;✅ Handle Failures Gracefully&lt;/p&gt;

&lt;p&gt;SMTP servers can throttle or reject.&lt;/p&gt;

&lt;p&gt;✅ Use Connection Pooling&lt;/p&gt;

&lt;p&gt;In high-volume systems.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
  pool: true,&lt;br&gt;
  maxConnections: 5,&lt;br&gt;
  maxMessages: 100&lt;br&gt;
}&lt;/p&gt;




&lt;p&gt;🧨 Why SMTP Config Fails&lt;/p&gt;

&lt;p&gt;Most common reasons:&lt;/p&gt;

&lt;p&gt;Port and secure mismatch&lt;/p&gt;

&lt;p&gt;Firewall blocking outbound SMTP&lt;/p&gt;

&lt;p&gt;Wrong credentials&lt;/p&gt;

&lt;p&gt;TLS version mismatch&lt;/p&gt;

&lt;p&gt;Provider rate limits&lt;/p&gt;

&lt;p&gt;Debug tip: Always enable logging during setup.&lt;/p&gt;




&lt;p&gt;🚀 Example: Clean Production Configuration&lt;/p&gt;

&lt;p&gt;{&lt;br&gt;
  host: process.env.SMTP_HOST,&lt;br&gt;
  port: 587,&lt;br&gt;
  secure: false,&lt;br&gt;
  auth: {&lt;br&gt;
    user: process.env.SMTP_USER,&lt;br&gt;
    pass: process.env.SMTP_PASS&lt;br&gt;
  },&lt;br&gt;
  tls: {&lt;br&gt;
    rejectUnauthorized: true&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Simple. Predictable. Stable.&lt;/p&gt;




&lt;p&gt;📌 Final Mental Model&lt;/p&gt;

&lt;p&gt;SMTP is not email marketing.&lt;br&gt;
SMTP is not analytics.&lt;br&gt;
SMTP is not inbox management.&lt;/p&gt;

&lt;p&gt;SMTP is transport.&lt;/p&gt;

&lt;p&gt;Think of it like TCP for email.&lt;br&gt;
Reliable when configured properly. Brutal when misconfigured. 📡&lt;/p&gt;




&lt;p&gt;If you want, I can also write:&lt;/p&gt;

&lt;p&gt;📊 A version comparing SMTP vs Email APIs&lt;/p&gt;

&lt;p&gt;🏗 A NestJS-specific SMTP implementation guide&lt;/p&gt;

&lt;p&gt;🔐 A deep dive into SPF/DKIM/DMARC&lt;/p&gt;

&lt;p&gt;Choose your weapon.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>networking</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Building My Portfolio Website: A Journey with React, GitHub Integration, and GitHub Pages Deployment</title>
      <dc:creator>Arjun Kumar</dc:creator>
      <pubDate>Wed, 25 Jun 2025 05:25:46 +0000</pubDate>
      <link>https://dev.to/arjun_computer_geek/building-my-portfolio-website-a-journey-with-react-github-integration-and-github-pages-deployment-1a66</link>
      <guid>https://dev.to/arjun_computer_geek/building-my-portfolio-website-a-journey-with-react-github-integration-and-github-pages-deployment-1a66</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I recently built my portfolio website at &lt;a href="https://arjun-computer-geek.github.io/" rel="noopener noreferrer"&gt;https://arjun-computer-geek.github.io/&lt;/a&gt; using modern web technologies and wanted to share the technical journey, challenges faced, and solutions implemented. This blog post covers everything from the tech stack to GitHub integration, Dev.to blog integration, and the deployment challenges I encountered.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎨 The Tech Stack &amp;amp; Design Philosophy
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Modern React with TypeScript
&lt;/h3&gt;

&lt;p&gt;I chose &lt;strong&gt;React 18&lt;/strong&gt; with &lt;strong&gt;TypeScript&lt;/strong&gt; for type safety and better developer experience. The project uses &lt;strong&gt;Vite&lt;/strong&gt; as the build tool for lightning-fast development and optimized production builds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Beautiful UI with shadcn/ui and Tailwind CSS
&lt;/h3&gt;

&lt;p&gt;The design system is built on &lt;strong&gt;shadcn/ui&lt;/strong&gt; components with &lt;strong&gt;Tailwind CSS&lt;/strong&gt; for styling. This combination provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consistent, accessible components&lt;/li&gt;
&lt;li&gt;Dark mode support out of the box&lt;/li&gt;
&lt;li&gt;Responsive design with utility classes&lt;/li&gt;
&lt;li&gt;Beautiful animations and transitions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key Design Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Glass morphism effects&lt;/strong&gt; with backdrop blur&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gradient backgrounds&lt;/strong&gt; and text effects&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Smooth animations&lt;/strong&gt; using CSS keyframes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Responsive grid layouts&lt;/strong&gt; for projects and blog posts&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interactive hover effects&lt;/strong&gt; and micro-interactions&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🔗 GitHub Integration: Real-time Project Data
&lt;/h2&gt;

&lt;h3&gt;
  
  
  GitHub API Integration
&lt;/h3&gt;

&lt;p&gt;One of the most exciting features is the real-time GitHub integration. I created a comprehensive GitHub service that fetches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Key features of the GitHub integration&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="nx"&gt;repositories&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;filtering &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;no&lt;/span&gt; &lt;span class="nx"&gt;forks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;no&lt;/span&gt; &lt;span class="nx"&gt;archived&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;only&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;GitHub&lt;/span&gt; &lt;span class="nf"&gt;statistics &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stars&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;forks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;languages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;Pinned&lt;/span&gt; &lt;span class="nx"&gt;repositories&lt;/span&gt; &lt;span class="nx"&gt;based&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;configuration&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;Experience&lt;/span&gt; &lt;span class="nx"&gt;calculation&lt;/span&gt; &lt;span class="nx"&gt;based&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;commit&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;Repository&lt;/span&gt; &lt;span class="nx"&gt;topics&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;metadata&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Implementation Details
&lt;/h3&gt;

&lt;p&gt;The GitHub integration uses &lt;strong&gt;React Query (TanStack Query)&lt;/strong&gt; for efficient data fetching with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Caching strategies&lt;/strong&gt; (5-minute stale time, 30-minute garbage collection)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retry logic&lt;/strong&gt; with exponential backoff&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error handling&lt;/strong&gt; and fallback states&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate limiting&lt;/strong&gt; considerations&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pinned Repositories System
&lt;/h3&gt;

&lt;p&gt;I implemented a smart pinning system that allows me to highlight specific projects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Configuration-based pinning&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PINNED_REPOSITORIES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;portfolio-website&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;awesome-project&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PINNED_TOPICS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;portfolio&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;showcase&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;featured&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This system automatically pins repositories based on their name or topics, making it easy to showcase my best work.&lt;/p&gt;

&lt;h2&gt;
  
  
  📝 Dev.to Blog Integration
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Real-time Blog Feed
&lt;/h3&gt;

&lt;p&gt;The portfolio includes a live feed of my Dev.to blog posts using their public API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Fetching Dev.to posts&lt;/span&gt;
&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://dev.to/api/articles?username=arjun_computer_geek&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Features Implemented
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Search functionality&lt;/strong&gt; across titles, descriptions, and tags&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Responsive grid layout&lt;/strong&gt; for blog cards&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tag filtering&lt;/strong&gt; and display&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pagination&lt;/strong&gt; with "View All" functionality&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error handling&lt;/strong&gt; and loading states&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Blog Card Design
&lt;/h3&gt;

&lt;p&gt;Each blog post card features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cover image support&lt;/li&gt;
&lt;li&gt;Publication date&lt;/li&gt;
&lt;li&gt;Tag display (with overflow handling)&lt;/li&gt;
&lt;li&gt;Hover effects and animations&lt;/li&gt;
&lt;li&gt;Direct link to Dev.to&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🚀 GitHub Pages Deployment Challenges &amp;amp; Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Routing Problem
&lt;/h3&gt;

&lt;p&gt;The biggest challenge was &lt;strong&gt;client-side routing&lt;/strong&gt; on GitHub Pages. GitHub Pages doesn't support server-side routing, which means direct navigation to routes like &lt;code&gt;/projects&lt;/code&gt; would result in a 404 error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution: HashRouter Implementation
&lt;/h3&gt;

&lt;p&gt;I implemented a dual-router system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Development: Clean URLs with BrowserRouter&lt;/span&gt;
&lt;span class="c1"&gt;// Production: Hash-based URLs with HashRouter&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;HashRouter&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BrowserRouter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  404.html Redirect Solution
&lt;/h3&gt;

&lt;p&gt;For direct navigation support, I created a &lt;code&gt;public/404.html&lt;/code&gt; file that handles redirects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- 404.html handles direct navigation --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/#&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  SPA Routing Solution for GitHub Pages
&lt;/h3&gt;

&lt;p&gt;I implemented a comprehensive SPA routing solution based on the &lt;a href="https://github.com/rafgraph/spa-github-pages" rel="noopener noreferrer"&gt;spa-github-pages&lt;/a&gt; approach. This solution handles both direct navigation and URL restoration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Script in index.html for SPA routing --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="c1"&gt;// Single Page Apps for GitHub Pages&lt;/span&gt;
  &lt;span class="c1"&gt;// MIT License&lt;/span&gt;
  &lt;span class="c1"&gt;// https://github.com/rafgraph/spa-github-pages&lt;/span&gt;
  &lt;span class="c1"&gt;// This script checks to see if a redirect is present in the query string,&lt;/span&gt;
  &lt;span class="c1"&gt;// converts it back into the correct url and adds it to the&lt;/span&gt;
  &lt;span class="c1"&gt;// browser's history using window.history.replaceState(...),&lt;/span&gt;
  &lt;span class="c1"&gt;// which won't cause the browser to attempt to load the new url.&lt;/span&gt;
  &lt;span class="c1"&gt;// When the single-page app is loaded further down in this file,&lt;/span&gt;
  &lt;span class="c1"&gt;// the correct url will be waiting in the browser's history for&lt;/span&gt;
  &lt;span class="c1"&gt;// the single-page app to route accordingly.&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/~and~/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replaceState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution works by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Detecting redirects&lt;/strong&gt; from the 404.html page&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Restoring the original URL&lt;/strong&gt; in the browser history&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Allowing React Router&lt;/strong&gt; to handle the routing properly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintaining clean URLs&lt;/strong&gt; for users while working with GitHub Pages limitations&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  How the Complete Routing Solution Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;User visits&lt;/strong&gt; &lt;code&gt;yoursite.com/projects&lt;/code&gt; directly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Pages&lt;/strong&gt; serves the 404.html file&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;404.html script&lt;/strong&gt; redirects to &lt;code&gt;yoursite.com/#/projects&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;index.html script&lt;/strong&gt; restores the URL to &lt;code&gt;yoursite.com/projects&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React Router&lt;/strong&gt; handles the routing to the Projects component&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This creates a seamless experience where users can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bookmark direct URLs&lt;/strong&gt; like &lt;code&gt;yoursite.com/projects&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Share clean links&lt;/strong&gt; without hash symbols&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Navigate directly&lt;/strong&gt; to any route&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintain browser history&lt;/strong&gt; properly&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  GitHub Actions Workflow
&lt;/h3&gt;

&lt;p&gt;I set up automated deployment using GitHub Actions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to GitHub Pages&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;main&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-node@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;node-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;18"&lt;/span&gt;
          &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;npm"&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm ci&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm run build&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/upload-pages-artifact@v3&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;./dist"&lt;/span&gt;

  &lt;span class="na"&gt;deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;github-pages&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/deploy-pages@v4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🎯 Key Features &amp;amp; Components
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Hero Section
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Animated gradient backgrounds&lt;/li&gt;
&lt;li&gt;Call-to-action buttons&lt;/li&gt;
&lt;li&gt;Social media links&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. About Section
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Personal introduction&lt;/li&gt;
&lt;li&gt;Skills overview&lt;/li&gt;
&lt;li&gt;Professional summary&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Experience Section
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Timeline-based experience display&lt;/li&gt;
&lt;li&gt;Company logos and descriptions&lt;/li&gt;
&lt;li&gt;Duration calculations&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Skills Section
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Categorized skill display&lt;/li&gt;
&lt;li&gt;Progress indicators&lt;/li&gt;
&lt;li&gt;Interactive hover effects&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Projects Section
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Featured projects showcase&lt;/li&gt;
&lt;li&gt;GitHub integration&lt;/li&gt;
&lt;li&gt;Live repository data&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. Blog Section
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Dev.to integration&lt;/li&gt;
&lt;li&gt;Search functionality&lt;/li&gt;
&lt;li&gt;Responsive grid layout&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. Contact Section
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Contact form&lt;/li&gt;
&lt;li&gt;Social media links&lt;/li&gt;
&lt;li&gt;Professional information&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🛠️ Development Tools &amp;amp; Workflow
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Lovable Integration
&lt;/h3&gt;

&lt;p&gt;I integrated &lt;strong&gt;Lovable&lt;/strong&gt; for component tagging and development workflow optimization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Vite config with Lovable&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;componentTagger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lovable-tagger&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;mode&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;react&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;mode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;componentTagger&lt;/span&gt;&lt;span class="p"&gt;()].&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nb"&gt;Boolean&lt;/span&gt;
  &lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📱 Responsive Design &amp;amp; Performance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mobile-First Approach
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Responsive breakpoints for all screen sizes&lt;/li&gt;
&lt;li&gt;Touch-friendly interactions&lt;/li&gt;
&lt;li&gt;Optimized layouts for mobile devices&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Performance Optimizations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code splitting&lt;/strong&gt; with React Router&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lazy loading&lt;/strong&gt; for components&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image optimization&lt;/strong&gt; and compression&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching strategies&lt;/strong&gt; for API calls&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎨 Design System &amp;amp; Styling
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Custom CSS Variables
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0%&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--foreground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;222.2&lt;/span&gt; &lt;span class="m"&gt;84%&lt;/span&gt; &lt;span class="m"&gt;4.9%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--primary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;222.2&lt;/span&gt; &lt;span class="m"&gt;47.4%&lt;/span&gt; &lt;span class="m"&gt;11.2%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--primary-foreground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;210&lt;/span&gt; &lt;span class="m"&gt;40%&lt;/span&gt; &lt;span class="m"&gt;98%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c"&gt;/* ... more variables */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Animation System
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="n"&gt;fade-in&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translateY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translateY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="n"&gt;glow&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%,&lt;/span&gt;
  &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;139&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;92&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;246&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="err"&gt;50&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;30px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;139&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;92&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;246&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔧 Configuration &amp;amp; Setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Environment Configuration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Base URL configuration for GitHub Pages&lt;/li&gt;
&lt;li&gt;API endpoints configuration&lt;/li&gt;
&lt;li&gt;Development vs production settings&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Build Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// vite.config.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;base&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;::&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;alias&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./src&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🚀 Deployment Process
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Build Process
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. GitHub Pages Configuration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Source: Deploy from a branch&lt;/li&gt;
&lt;li&gt;Branch: main&lt;/li&gt;
&lt;li&gt;Folder: / (root)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Automatic Deployment
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;GitHub Actions triggers on push to main&lt;/li&gt;
&lt;li&gt;Builds the project&lt;/li&gt;
&lt;li&gt;Deploys to GitHub Pages&lt;/li&gt;
&lt;li&gt;Updates live site automatically&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎯 Lessons Learned &amp;amp; Best Practices
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Router Configuration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Always test routing in production environment&lt;/li&gt;
&lt;li&gt;Use HashRouter for GitHub Pages&lt;/li&gt;
&lt;li&gt;Implement proper 404 handling&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. API Integration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Implement proper error handling&lt;/li&gt;
&lt;li&gt;Use caching strategies&lt;/li&gt;
&lt;li&gt;Consider rate limiting&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Performance
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Optimize bundle size&lt;/li&gt;
&lt;li&gt;Implement lazy loading&lt;/li&gt;
&lt;li&gt;Use proper caching headers&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. User Experience
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Provide loading states&lt;/li&gt;
&lt;li&gt;Handle error scenarios gracefully&lt;/li&gt;
&lt;li&gt;Ensure accessibility&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🔮 Future Enhancements
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Planned Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Blog CMS integration&lt;/strong&gt; for custom blog posts&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analytics dashboard&lt;/strong&gt; for portfolio insights&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dark/Light mode toggle&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internationalization&lt;/strong&gt; support&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PWA capabilities&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Technical Improvements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server-side rendering&lt;/strong&gt; for better SEO&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image optimization&lt;/strong&gt; with next-gen formats&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Advanced caching&lt;/strong&gt; strategies&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Performance monitoring&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  📚 Resources &amp;amp; References
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Technologies Used
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;React 18&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://vitejs.dev/" rel="noopener noreferrer"&gt;Vite&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://tailwindcss.com/" rel="noopener noreferrer"&gt;Tailwind CSS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ui.shadcn.com/" rel="noopener noreferrer"&gt;shadcn/ui&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://tanstack.com/query" rel="noopener noreferrer"&gt;React Query&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.github.com/en/rest" rel="noopener noreferrer"&gt;GitHub API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Deployment Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pages.github.com/" rel="noopener noreferrer"&gt;GitHub Pages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/features/actions" rel="noopener noreferrer"&gt;GitHub Actions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactrouter.com/" rel="noopener noreferrer"&gt;React Router&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎉 Conclusion
&lt;/h2&gt;

&lt;p&gt;Building this portfolio website has been an incredible learning experience. The combination of modern React practices, beautiful UI design, and seamless GitHub integration creates a powerful showcase for my work. The challenges with GitHub Pages routing taught me valuable lessons about deployment strategies and user experience considerations.&lt;/p&gt;

&lt;p&gt;The live GitHub integration and Dev.to blog feed make the portfolio dynamic and always up-to-date, while the beautiful design system ensures a professional and engaging user experience.&lt;/p&gt;

&lt;p&gt;You can visit the live portfolio at: &lt;strong&gt;&lt;a href="https://arjun-computer-geek.github.io/" rel="noopener noreferrer"&gt;https://arjun-computer-geek.github.io/&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can explore the complete source code here: &lt;strong&gt;&lt;a href="https://github.com/arjun-computer-geek/arjun-computer-geek.github.io" rel="noopener noreferrer"&gt;https://github.com/arjun-computer-geek/arjun-computer-geek.github.io&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Feel free to explore the codebase and reach out if you have any questions about the implementation!&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This blog post covers the technical implementation of my portfolio website. The project demonstrates modern web development practices, API integration, and deployment strategies that can be applied to other projects.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>github</category>
      <category>react</category>
      <category>portfolio</category>
    </item>
    <item>
      <title>🧠 Mastering MongoDB Aggregation</title>
      <dc:creator>Arjun Kumar</dc:creator>
      <pubDate>Thu, 01 May 2025 04:58:18 +0000</pubDate>
      <link>https://dev.to/arjun_computer_geek/mastering-mongodb-aggregation-4094</link>
      <guid>https://dev.to/arjun_computer_geek/mastering-mongodb-aggregation-4094</guid>
      <description>&lt;p&gt;In this post, we dive deep into MongoDB’s aggregation framework using a practical dataset of &lt;code&gt;users&lt;/code&gt; and &lt;code&gt;orders&lt;/code&gt;. Below are 8 real-world questions and how you can solve them using stages like &lt;code&gt;$lookup&lt;/code&gt;, &lt;code&gt;$unwind&lt;/code&gt;, &lt;code&gt;$group&lt;/code&gt;, and &lt;code&gt;$project&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;📦 Sample Data&lt;br&gt;
📄 Users Collection&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  { "_id": 1, "name": "Alice", "country": "USA" },
  { "_id": 2, "name": "Bob", "country": "India" },
  { "_id": 3, "name": "Charlie", "country": "India" },
  { "_id": 4, "name": "Diana", "country": "USA" },
  { "_id": 5, "name": "Eve", "country": "UK" }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📄 Orders Collection&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  { "_id": 1, "userId": 1, "amount": 200 },
  { "_id": 2, "userId": 2, "amount": 350 },
  { "_id": 3, "userId": 1, "amount": 150 },
  { "_id": 4, "userId": 3, "amount": 400 },
  { "_id": 5, "userId": 3, "amount": 100 },
  { "_id": 6, "userId": 4, "amount": 300 },
  { "_id": 7, "userId": 5, "amount": 50 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔍 Question 1: Join users with their orders&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Result: Each user now has an orders array.&lt;/p&gt;

&lt;p&gt;🔍 Question 2: Unwind orders array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  {
    $unwind: "$orders"
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Result: Each user-order combination is flattened into individual documents.&lt;/p&gt;

&lt;p&gt;🔍 Question 3: Total amount spent by each user&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  {
    $unwind: "$orders"
  },
  {
    $group: {
      _id: "$_id",
      totalSpent: { $sum: "$orders.amount" },
      name: { $first: "$name" }
    }
  },
  {
    $project: {
      _id: 0,
      name: 1,
      totalSpent: 1
    }
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔍 Question 4: Average order amount per country&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  { $unwind: "$orders" },
  {
    $group: {
      _id: "$country",
      avgSpent: { $avg: "$orders.amount" }
    }
  },
  {
    $project: {
      country: "$_id",
      avgSpent: 1,
      _id: 0
    }
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔍 Question 5: Most expensive user (by total spent)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  { $unwind: "$orders" },
  {
    $group: {
      _id: "$name",
      totalSpent: { $sum: "$orders.amount" }
    }
  },
  { $sort: { totalSpent: -1 } },
  { $limit: 1 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Result: The user who spent the most.&lt;/p&gt;

&lt;p&gt;🔍 Question 6: Count how many orders each user made&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  {
    $project: {
      name: 1,
      orderCount: { $size: "$orders" }
    }
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔍 Question 7: User with highest average order value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  {
    $project: {
      name: 1,
      avgOrderValue: { $avg: "$orders.amount" }
    }
  },
  { $sort: { avgOrderValue: -1 } },
  { $limit: 1 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔍 Question 8: Total revenue per country&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  { $unwind: "$orders" },
  {
    $group: {
      _id: "$country",
      revenue: { $sum: "$orders.amount" }
    }
  },
  {
    $project: {
      country: "$_id",
      revenue: 1,
      _id: 0
    }
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🚀 Conclusion
&lt;/h2&gt;

&lt;p&gt;These examples reflect real scenarios you might encounter while analyzing user and order data. MongoDB's aggregation pipeline is extremely flexible for joining, transforming, and summarizing data — no need to switch to SQL for analytics anymore.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🚀 MongoDB Aggregation Challenge - Lookup &amp; Grouping Mastery 🔥</title>
      <dc:creator>Arjun Kumar</dc:creator>
      <pubDate>Tue, 29 Apr 2025 05:03:11 +0000</pubDate>
      <link>https://dev.to/arjun_computer_geek/mongodb-aggregation-challenge-lookup-grouping-mastery-5972</link>
      <guid>https://dev.to/arjun_computer_geek/mongodb-aggregation-challenge-lookup-grouping-mastery-5972</guid>
      <description>&lt;p&gt;Today’s practice involved joining two collections (&lt;code&gt;users&lt;/code&gt; and &lt;code&gt;orders&lt;/code&gt;) and mastering lookup, unwind, group, sort, and limit stages in aggregation pipelines!&lt;/p&gt;

&lt;p&gt;Here’s the full practice breakdown ⤵️&lt;/p&gt;

&lt;p&gt;🗄️ Database Structure:&lt;br&gt;
Collection: &lt;code&gt;users&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  { "_id": 1, "name": "Alice", "country": "USA" },
  { "_id": 2, "name": "Bob", "country": "Canada" },
  { "_id": 3, "name": "Charlie", "country": "India" }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Collection: &lt;code&gt;orders&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  { "_id": 101, "userId": 1, "amount": 250 },
  { "_id": 102, "userId": 1, "amount": 300 },
  { "_id": 103, "userId": 2, "amount": 400 },
  { "_id": 104, "userId": 3, "amount": 150 },
  { "_id": 105, "userId": 3, "amount": 200 }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧩 Challenge Questions and Answers:
&lt;/h2&gt;

&lt;p&gt;1️⃣ Join users with their orders&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Result: Each user document will now have an array of their related orders under "orders".&lt;/p&gt;

&lt;p&gt;2️⃣ Unwind the orders&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  {
    $unwind: "$orders"
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Result: Each order becomes a separate document with user data attached (no longer an array).&lt;/p&gt;

&lt;p&gt;3️⃣ Calculate total spend by each user&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  {
    $unwind: "$orders"
  },
  {
    $group: {
      _id: "$_id",
      totalSpend: { $sum: "$orders.amount" },
      name: { $first: "$name" }
    }
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Result: Each user now has a totalSpend field showing how much they spent in total!&lt;/p&gt;

&lt;p&gt;4️⃣ Calculate average spend per country&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  {
    $unwind: "$orders"
  },
  {
    $group: {
      _id: "$country",
      avgSpent: { $avg: "$orders.amount" }
    }
  },
  {
    $project: {
      country: "$_id",
      avgSpent: 1,
      _id: 0
    }
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Result: Average order amount for each country!&lt;/p&gt;

&lt;p&gt;5️⃣ Find the user who spent the most (Most Expensive User)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    $lookup: {
      from: "orders",
      localField: "_id",
      foreignField: "userId",
      as: "orders"
    }
  },
  {
    $unwind: "$orders"
  },
  {
    $group: {
      _id: "$_id",
      totalSpend: { $sum: "$orders.amount" },
      name: { $first: "$name" }
    }
  },
  {
    $sort: {
      totalSpend: -1
    }
  },
  {
    $limit: 1
  },
  {
    $project: {
      name: 1,
      totalSpend: 1,
      _id: 0
    }
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Result: The top spender in the users list!&lt;/p&gt;

&lt;p&gt;🎯 Key MongoDB Operators Practiced:&lt;/p&gt;

&lt;p&gt;$lookup ➔ for joining collections&lt;br&gt;
$unwind ➔ flatten arrays&lt;br&gt;
$group ➔ aggregate data&lt;br&gt;
$sort ➔ order results&lt;br&gt;
$limit ➔ restrict number of outputs&lt;br&gt;
$project ➔ customize final output&lt;/p&gt;

&lt;p&gt;💬 Final Thoughts:&lt;br&gt;
Working with lookup and aggregation pipelines makes MongoDB a very powerful tool for analytics, reporting, and complex data transformations!&lt;/p&gt;

&lt;p&gt;🔥 Practice like this strengthens your real-world NoSQL database skills!&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>nosql</category>
      <category>webdev</category>
      <category>database</category>
    </item>
    <item>
      <title>Mastering MongoDB Aggregation: 10 Practical Examples with Sample Data</title>
      <dc:creator>Arjun Kumar</dc:creator>
      <pubDate>Thu, 24 Apr 2025 05:40:52 +0000</pubDate>
      <link>https://dev.to/arjun_computer_geek/mastering-mongodb-aggregation-10-practical-examples-with-sample-data-3ek7</link>
      <guid>https://dev.to/arjun_computer_geek/mastering-mongodb-aggregation-10-practical-examples-with-sample-data-3ek7</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpac78ksv5liyp5y9mcvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpac78ksv5liyp5y9mcvz.png" alt="Image description" width="800" height="1200"&gt;&lt;/a&gt;MongoDB's aggregation framework is powerful yet often misunderstood. Whether you're prepping for interviews, building analytics, or learning data science workflows, mastering aggregation is essential. Below are 10 real-world problems, complete with queries and explanations, using a sample dataset of users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sample MongoDB Collection: users
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  { "name": "Alice", "age": 28, "country": "USA", "gender": "female", "signupDate": "2023-12-20T10:15:00Z" },
  { "name": "Bob", "age": 34, "country": "Canada", "gender": "male", "signupDate": "2024-01-15T12:00:00Z" },
  { "name": "Charlie", "age": 22, "country": "India", "gender": "male", "signupDate": "2024-02-05T09:45:00Z" },
  { "name": "Diana", "age": 30, "country": "USA", "gender": "female", "signupDate": "2024-03-01T08:30:00Z" },
  { "name": "Eve", "age": 29, "country": "India", "gender": "female", "signupDate": "2024-01-25T11:20:00Z" },
  { "name": "Frank", "age": 40, "country": "Canada", "gender": "male", "signupDate": "2024-02-10T14:50:00Z" },
  { "name": "Grace", "age": 19, "country": "USA", "gender": "female", "signupDate": "2024-03-10T07:10:00Z" },
  { "name": "Harry", "age": 31, "country": "UK", "gender": "male", "signupDate": "2024-03-12T17:40:00Z" },
  { "name": "Ivy", "age": 27, "country": "India", "gender": "female", "signupDate": "2024-01-01T06:00:00Z" },
  { "name": "Jack", "age": 36, "country": "Canada", "gender": "male", "signupDate": "2024-02-20T18:15:00Z" }
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  1. Count of Users by Country
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$country&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;totalUsers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Average Age by Gender
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$gender&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;averageAge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$avg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Users Signed Up After February 1, 2024
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$addFields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;signupDateConverted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;$toDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$signupDate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;signupDateConverted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;$gte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ISODate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2024-02-01T00:00:00Z&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Count of Female Users by Country
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;female&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$country&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;femalePerCountry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Group by Country After Sorting by Signup Date (Descending)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sort&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;signupDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$country&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Get the 3 Earliest Signups
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sort&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;signupDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. All Indian Users Sorted by Age Descending
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;India&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sort&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  8. Count of Users Grouped by Month of Signup
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$project&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;monthYear&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;$dateToString&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;%Y-%m&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$toDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$signupDate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$monthYear&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;userCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sort&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  9. Average Age of Users Signed Up in 2024
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$project&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;$dateToString&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;%Y&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$toDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$signupDate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;signupDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2024&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$year&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;avgAge&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$avg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10. Count of Users Aged 25 to 35 Grouped by Gender
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$gte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;$lte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$gender&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;MongoDB aggregations are incredibly versatile. From basic groupings to complex date manipulations, they can power dashboards, reports, and analytics. Bookmark this post for reference and practice daily!&lt;/p&gt;

&lt;p&gt;Happy querying! 🚀&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>aggregationpipeline</category>
      <category>fullstackdevelopmen</category>
      <category>nosql</category>
    </item>
    <item>
      <title>Passing Data from Child to Parent in Svelte: A Comprehensive Guide</title>
      <dc:creator>Arjun Kumar</dc:creator>
      <pubDate>Thu, 19 Sep 2024 13:22:56 +0000</pubDate>
      <link>https://dev.to/arjun_computer_geek/passing-data-from-child-to-parent-in-svelte-a-comprehensive-guide-318g</link>
      <guid>https://dev.to/arjun_computer_geek/passing-data-from-child-to-parent-in-svelte-a-comprehensive-guide-318g</guid>
      <description>&lt;p&gt;In Svelte, data flow is generally one-directional—from parent to child. However, there are times when we need to pass data from child to parent, and we can use custom events for this. Another convenient way to synchronize data between child and parent is through two-way binding. This allows the parent to maintain control over the data, while the child updates it.&lt;/p&gt;

&lt;p&gt;In this blog post, we will explore two methods for passing data from child to parent: using custom events and using two-way binding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 1: Passing Data with Custom Events
&lt;/h2&gt;

&lt;p&gt;Let's first see how to pass data from a child to a parent using custom events. In this scenario, we will use a simple counter component that sends updated count data to the parent component.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: The Child Component
&lt;/h3&gt;

&lt;p&gt;In the child component (&lt;code&gt;Counter.svelte&lt;/code&gt;), we define a counter and use Svelte's &lt;code&gt;createEventDispatcher&lt;/code&gt; to emit an event to the parent when the count is updated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    import { createEventDispatcher } from 'svelte';

    const dispatch = createEventDispatcher();
    let count = 0;

    function increment() {
        count += 1;
        dispatch('increment', { count });
    }
&amp;lt;/script&amp;gt;

&amp;lt;button on:click={increment}&amp;gt;
    Count: {count}
&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: The Parent Component
&lt;/h3&gt;

&lt;p&gt;In the parent component, we listen for the custom &lt;code&gt;increment&lt;/code&gt; event and update the parent's state accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    let totalCount = 0;

    function handleIncrement(event) {
        totalCount = event.detail.count;
    }
&amp;lt;/script&amp;gt;

&amp;lt;h1&amp;gt;Total Count: {totalCount}&amp;lt;/h1&amp;gt;
&amp;lt;Counter on:increment={handleIncrement} /&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Method 2: Two-Way Binding
&lt;/h2&gt;

&lt;p&gt;Another approach to pass data between child and parent components is by using two-way binding. This allows the parent component to directly bind a variable to the child component's property, and any changes in the child will automatically update the parent.&lt;/p&gt;

&lt;p&gt;Let's see how this works with an example where we bind a name property between parent and child components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: The Child Component (&lt;code&gt;Box.svelte&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Here, the child component accepts a &lt;code&gt;person&lt;/code&gt; prop, which is bound to a parent variable using &lt;code&gt;bind:&lt;/code&gt; syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    export let person;
&amp;lt;/script&amp;gt;

&amp;lt;input bind:value={person} placeholder="Enter your name" /&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: The Parent Component
&lt;/h3&gt;

&lt;p&gt;In the parent component, we declare a variable &lt;code&gt;p&lt;/code&gt; and bind it to the &lt;code&gt;person&lt;/code&gt; prop in the &lt;code&gt;Box&lt;/code&gt; component. We also create a reactive statement to display the uppercase version of the name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    import Box from './Box.svelte';
    let p = 'MyName';

    // Reactive statement to compute the uppercase version of p
    $: nameUpper = p.toUpperCase();
&amp;lt;/script&amp;gt;

&amp;lt;!-- Bind p to person in the child component --&amp;gt;
&amp;lt;Box bind:person={p} /&amp;gt;

&amp;lt;p&amp;gt;Reactive value in the parent component: {nameUpper}&amp;lt;/p&amp;gt;

&amp;lt;hr /&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Reactive Statement: We use the reactive &lt;code&gt;$:&lt;/code&gt; syntax in Svelte to automatically update the &lt;code&gt;nameUpper&lt;/code&gt; variable whenever &lt;code&gt;p&lt;/code&gt; changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Two-Way Binding: By using the &lt;code&gt;bind:&lt;/code&gt; directive in the parent (&lt;code&gt;&amp;lt;Box bind:person={p} /&amp;gt;&lt;/code&gt;), any change in the child component's &lt;code&gt;person&lt;/code&gt; prop will automatically update &lt;code&gt;p&lt;/code&gt; in the parent.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Example in Action:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Initially, the parent &lt;code&gt;p&lt;/code&gt; is set to &lt;code&gt;"MyName"&lt;/code&gt;. This is passed to the child, and the input box displays &lt;code&gt;"MyName"&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you type a new name in the input box, the &lt;code&gt;person&lt;/code&gt; variable in the child updates, which also updates &lt;code&gt;p&lt;/code&gt; in the parent. This change triggers the reactive statement &lt;code&gt;$: nameUpper = p.toUpperCase()&lt;/code&gt; in the parent, and the uppercase name is displayed in real-time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;By using custom events and two-way binding, you can easily pass data from child to parent components in Svelte. For more complex or multi-component data sharing, you can consider using stores. However, for simple parent-child communication, these techniques are effective and easy to implement.&lt;/p&gt;

&lt;p&gt;In summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;custom events&lt;/strong&gt; when you need to notify the parent of a change in the child component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;two-way binding&lt;/strong&gt; when you want to directly synchronize a variable between the parent and child.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>svelte</category>
      <category>sveltekit</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to solve Authentication Issues When Deploying Node.js and Svelte to Production on Heroku?</title>
      <dc:creator>Arjun Kumar</dc:creator>
      <pubDate>Thu, 12 Sep 2024 13:18:30 +0000</pubDate>
      <link>https://dev.to/arjun_computer_geek/how-to-solve-authentication-issues-when-deploying-nodejs-and-svelte-to-production-on-heroku-4ec8</link>
      <guid>https://dev.to/arjun_computer_geek/how-to-solve-authentication-issues-when-deploying-nodejs-and-svelte-to-production-on-heroku-4ec8</guid>
      <description>&lt;p&gt;As developers, we often encounter unexpected challenges when moving our applications from local development to production environments. Recently, I faced a perplexing issue with authentication in a Node.js and Svelte application using Passport.js. Here's the journey of troubleshooting and the surprising solution that finally resolved the problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Setup
&lt;/h2&gt;

&lt;p&gt;Our application stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Backend: Node.js with Express&lt;/li&gt;
&lt;li&gt;Frontend: Svelte&lt;/li&gt;
&lt;li&gt;Authentication: Passport.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything worked flawlessly in the local development environment. The authentication flow was smooth, and we were ready to deploy to production. Little did we know the hurdles that awaited us.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Production Puzzle
&lt;/h2&gt;

&lt;p&gt;Upon deploying to production (in this case, Heroku), our authentication suddenly stopped working. The application that ran perfectly on localhost was now failing in the real world. It was time to put on our detective hats and dive into troubleshooting mode.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Troubleshooting Journey
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. CORS Issues
&lt;/h3&gt;

&lt;p&gt;The first suspect was CORS (Cross-Origin Resource Sharing). We double-checked our CORS configuration on the backend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use(cors({ origin: 'https://your-frontend-url.com', credentials: true }));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Fetch API Credentials
&lt;/h3&gt;

&lt;p&gt;We ensured that our frontend Fetch API calls included the credentials: 'include' option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch('https://your-api-url.com/login', {
  method: 'POST',
  credentials: 'include',
  // other options...
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Vite Config Proxy
&lt;/h3&gt;

&lt;p&gt;We even tried setting up a proxy in our Svelte app's vite.config.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default defineConfig({
  // other config...
  server: {
    proxy: {
      '/api': {
        target: 'https://your-api-url.com',
        changeOrigin: true,
        secure: false,
      }
    }
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Cookie Settings
&lt;/h3&gt;

&lt;p&gt;We experimented with various cookie settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use(session({
  // other options...
  cookie: {
    secure: true, // We tried both true and false
    sameSite: 'none', // We tried different options here
  }
}));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Unexpected Solution
&lt;/h2&gt;

&lt;p&gt;After hours of frustration and countless blog posts, the solution emerged from an unexpected place. The issue wasn't with CORS, fetch credentials, or cookie settings. The culprit was a single line of code that we hadn't considered:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.set('trust proxy', 1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line is crucial when your application is behind a reverse proxy, which is the case with Heroku. Without it, Express doesn't know it's behind a proxy and can't properly set secure cookies or determine the correct protocol (http vs https).&lt;/p&gt;

&lt;h2&gt;
  
  
  Why This Worked
&lt;/h2&gt;

&lt;p&gt;Heroku uses a reverse proxy to handle incoming requests before they reach your application. This means that from your app's perspective, all requests appear to come from the proxy rather than the original client.&lt;/p&gt;

&lt;p&gt;By setting &lt;code&gt;trust proxy&lt;/code&gt;, we're telling Express to trust the  &lt;code&gt;X-Forwarded-*&lt;/code&gt; headers set by the proxy. This allows Express to:   &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Correctly determine if the connection is secure (https).&lt;/li&gt;
&lt;li&gt;Set the secure flag on cookies when appropriate.&lt;/li&gt;
&lt;li&gt;Use the correct IP address for request logging and other purposes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Understand Your Hosting Environment: Different hosting platforms may have unique configurations that affect your application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Read Documentation Thoroughly: The solution was mentioned in Express.js documentation, but it's easy to overlook when you're not specifically looking for it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don't Assume: Just because something works locally doesn't mean it will work in production without adjustments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keep Learning: The web development landscape is vast, and there's always more to learn about the intricacies of deployment and production environments.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;While troubleshooting can be frustrating, it's also an opportunity to deepen our understanding of the systems we work with. This experience highlighted the importance of considering the entire stack, including the hosting environment, when deploying web applications.&lt;/p&gt;

&lt;p&gt;Remember, if you're deploying to a platform that uses a reverse proxy (like Heroku), don't forget to set &lt;code&gt;trust proxy&lt;/code&gt; in your Express application. It might just save you hours of debugging!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React Lifecycle methods</title>
      <dc:creator>Arjun Kumar</dc:creator>
      <pubDate>Sat, 23 Jul 2022 14:03:00 +0000</pubDate>
      <link>https://dev.to/arjun_computer_geek/react-lifecycle-methods-1051</link>
      <guid>https://dev.to/arjun_computer_geek/react-lifecycle-methods-1051</guid>
      <description>&lt;p&gt;&lt;strong&gt;Are you unable to understand these lifecycle of component?&lt;/strong&gt;&lt;br&gt;
So here I am. I will explain the concept of react lifecycle.&lt;/p&gt;

&lt;p&gt;In this post, you'll learn more about the React component lifecycle and the different methods within each phase (for class-based components), focusing particularly on methods and hooks.&lt;/p&gt;


&lt;h2&gt;
  
  
  Lifecycle of Components
&lt;/h2&gt;

&lt;p&gt;Each component in React has a lifecycle which you can monitor and manipulate during its three main phases.&lt;/p&gt;

&lt;p&gt;The three phases are: &lt;code&gt;Mounting&lt;/code&gt;, &lt;code&gt;Updating&lt;/code&gt;, and &lt;code&gt;Unmounting&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can see the visual representation of these methods in below image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftjq8awjc2gjx6ziiq6tb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftjq8awjc2gjx6ziiq6tb.jpg" alt="react-lifecycle-methods" width="800" height="442"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  Mounting Phase
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;mounting phase&lt;/strong&gt; is when a new component is created and inserted into the DOM or, in other words, when the life of a component begins. This can only happen once, and is often called “&lt;em&gt;initial render&lt;/em&gt;.”&lt;/p&gt;

&lt;p&gt;React has four built-in methods that gets called, in this order, when mounting a component:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;constructor()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;getDerivedStateFromProps()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;render()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentDidMount()&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;render()&lt;/code&gt; method is required and will always be called, the others are optional and will be called if we define them.&lt;/p&gt;


&lt;h4&gt;
  
  
  1. constructor
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;constructor()&lt;/code&gt; method is called before anything else, when the component is initiated, and it is the natural place to set up the initial state and other initial values.&lt;/p&gt;
&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;constructor&lt;/code&gt; method is called, by React, every time you make a component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favoritecolor: "red"};
  }
  render() {
    return (
      &amp;lt;h1&amp;gt;My Favorite Color is {this.state.favoritecolor}&amp;lt;/h1&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  2. getDerivedStateFromProps
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;getDerivedStateFromProps()&lt;/code&gt; method is called right before rendering the element(s) in the DOM.&lt;/p&gt;

&lt;p&gt;This is the natural place to set the state object based on the initial props.&lt;/p&gt;

&lt;p&gt;In many cases, your component’s state will be &lt;em&gt;derivative&lt;/em&gt; of its props. This is where the &lt;code&gt;getDerivedStateFromProps&lt;/code&gt; method comes in. This method allows you to modify the &lt;code&gt;state&lt;/code&gt; value with any &lt;code&gt;props&lt;/code&gt; value. It's most useful for changes in props over time, and we’ll learn later that it’s also useful in the update phase.&lt;/p&gt;

&lt;p&gt;It takes state as an argument, and returns an object with changes to the state.&lt;/p&gt;

&lt;p&gt;The method &lt;code&gt;getDerivedStateFromProps&lt;/code&gt; accepts two arguments: &lt;code&gt;props&lt;/code&gt; and &lt;code&gt;state&lt;/code&gt;, and returns an object, or &lt;code&gt;null&lt;/code&gt; if no change is needed. These values are passed directly to the method, so there’s no need for it to have access to the instance of the class (or any other part of the class) and thus is considered a static method.&lt;/p&gt;

&lt;p&gt;The example below starts with the favorite color being "red", but the &lt;code&gt;getDerivedStateFromProps()&lt;/code&gt; method updates the favorite color based on the &lt;code&gt;favcol&lt;/code&gt; attribute:&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;getDerivedStateFromProps&lt;/code&gt; method is called right before the render method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favoritecolor: "red"};
  }
  static getDerivedStateFromProps(props, state) {
    return {favoritecolor: props.favcol };
  }
  render() {
    return (
      &amp;lt;h1&amp;gt;My Favorite Color is {this.state.favoritecolor}&amp;lt;/h1&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header favcol="yellow"/&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  3. render
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;render()&lt;/code&gt; method is required, and is the method that actually outputs the HTML to the DOM.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;A simple component with a simple &lt;code&gt;render()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  render() {
    return (
      &amp;lt;h1&amp;gt;This is the content of the Header component&amp;lt;/h1&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  4. componentDidMount
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;componentDidMount()&lt;/code&gt; method is called after the component is rendered.&lt;br&gt;
&lt;code&gt;componentDidMount&lt;/code&gt; is the last lifecycle method called in the mounting phase. It’s called right after the component is rendered or mounted to the DOM.&lt;/p&gt;

&lt;p&gt;With this method, you're allowed to add side effects like sending network requests or updating the component's state. Additionally, the &lt;code&gt;componentDidMount&lt;/code&gt; method allows you to make subscriptions like subscribing to the Redux store. You can also call the &lt;code&gt;this.setState&lt;/code&gt; method right away; however this will cause a re-render as it kicks in the update phase, since the state has changed.&lt;/p&gt;
&lt;h5&gt;
  
  
  Note :
&lt;/h5&gt;

&lt;p&gt;You need to be careful with &lt;code&gt;componentDidMount&lt;/code&gt; because it may cause unnecessary re-renders.&lt;/p&gt;
&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;At first my favorite color is red, but give me a second, and it is yellow instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favoritecolor: "red"};
  }
  componentDidMount() {
    setTimeout(() =&amp;gt; {
      this.setState({favoritecolor: "yellow"})
    }, 1000)
  }
  render() {
    return (
      &amp;lt;h1&amp;gt;My Favorite Color is {this.state.favoritecolor}&amp;lt;/h1&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Updating Phase
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Updating phase&lt;/strong&gt; is triggered when component's props or state changes.&lt;br&gt;
React has five built-in methods that gets called, in this order, when a component is updated:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;getDerivedStateFromProps()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;shouldComponentUpdate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;render()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;getSnapshotBeforeUpdate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentDidUpdate()&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;render()&lt;/code&gt; method is required and will always be called, the others are optional and will be called if you define them.&lt;/p&gt;


&lt;h4&gt;
  
  
  1. getDerivedStateFromProps
&lt;/h4&gt;

&lt;p&gt;In the update phase, the first lifecycle method called is &lt;code&gt;getDerivedStateFromProps&lt;/code&gt;. This method is useful if you have updated &lt;code&gt;props&lt;/code&gt; and you want to reflect that in the component's state.&lt;/p&gt;

&lt;p&gt;For instance, a component’s &lt;code&gt;state&lt;/code&gt; may depend on the value of its &lt;code&gt;props&lt;/code&gt;. With &lt;code&gt;getDerivedStateFromProps&lt;/code&gt;, before the component was even re-rendered, its state can reflect those changes and can be shown (if applicable) to the newly updated component.&lt;/p&gt;

&lt;p&gt;This is still the natural place to set the &lt;code&gt;state&lt;/code&gt; object based on the initial props.&lt;/p&gt;

&lt;p&gt;The example below has a button that changes the favorite color to blue, but since the &lt;code&gt;getDerivedStateFromProps()&lt;/code&gt; method is called, which updates the state with the color from the favcol attribute, the favorite color is still rendered as yellow:&lt;/p&gt;
&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;If the component gets updated, the &lt;code&gt;getDerivedStateFromProps()&lt;/code&gt; method is called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favoritecolor: "red"};
  }
  static getDerivedStateFromProps(props, state) {
    return {favoritecolor: props.favcol };
  }
  changeColor = () =&amp;gt; {
    this.setState({favoritecolor: "blue"});
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;My Favorite Color is {this.state.favoritecolor}&amp;lt;/h1&amp;gt;
      &amp;lt;button type="button" onClick={this.changeColor}&amp;gt;Change color&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header favcol="yellow"/&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  2. shouldComponentUpdate
&lt;/h4&gt;

&lt;p&gt;In the &lt;code&gt;shouldComponentUpdate()&lt;/code&gt; method you can return a Boolean value that specifies whether React should continue with the rendering or not.&lt;/p&gt;

&lt;p&gt;The default value is &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The example below shows what happens when the &lt;code&gt;shouldComponentUpdate()&lt;/code&gt; method returns &lt;code&gt;false&lt;/code&gt;:&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;Stop the component from rendering at any update:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favoritecolor: "red"};
  }
  shouldComponentUpdate() {
    return false;
  }
  changeColor = () =&amp;gt; {
    this.setState({favoritecolor: "blue"});
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;My Favorite Color is {this.state.favoritecolor}&amp;lt;/h1&amp;gt;
      &amp;lt;button type="button" onClick={this.changeColor}&amp;gt;Change color&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;Same example as above, but this time the shouldComponentUpdate() method returns true instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favoritecolor: "red"};
  }
  shouldComponentUpdate() {
    return true;
  }
  changeColor = () =&amp;gt; {
    this.setState({favoritecolor: "blue"});
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;My Favorite Color is {this.state.favoritecolor}&amp;lt;/h1&amp;gt;
      &amp;lt;button type="button" onClick={this.changeColor}&amp;gt;Change color&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  3. render
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;render()&lt;/code&gt; method is of course called when a component gets updated, it has to re-render the HTML to the DOM, with the new changes.&lt;/p&gt;

&lt;p&gt;The example below has a button that changes the favorite color to blue:&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;Click the button to make a change in the component's state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favoritecolor: "red"};
  }
  changeColor = () =&amp;gt; {
    this.setState({favoritecolor: "blue"});
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;My Favorite Color is {this.state.favoritecolor}&amp;lt;/h1&amp;gt;
      &amp;lt;button type="button" onClick={this.changeColor}&amp;gt;Change color&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  4. getSnapshotBeforeUpdate
&lt;/h4&gt;

&lt;p&gt;In the &lt;code&gt;getSnapshotBeforeUpdate()&lt;/code&gt; method you have access to the &lt;code&gt;props&lt;/code&gt; and &lt;code&gt;state&lt;/code&gt; before the update, meaning that even after the update, you can check what the values were before the update.&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;getSnapshotBeforeUpdate()&lt;/code&gt; method is present, you should also include the &lt;code&gt;componentDidUpdate()&lt;/code&gt; method, otherwise you will get an error.&lt;/p&gt;

&lt;p&gt;The example below might seem complicated, but all it does is this:&lt;/p&gt;

&lt;p&gt;When the component is mounting it is rendered with the favorite color "red".&lt;/p&gt;

&lt;p&gt;When the component has been mounted, a timer changes the state, and after one second, the favorite color becomes "yellow".&lt;/p&gt;

&lt;p&gt;This action triggers the &lt;em&gt;update&lt;/em&gt; phase, and since this component has a &lt;code&gt;getSnapshotBeforeUpdate()&lt;/code&gt; method, this method is executed, and writes a message to the empty DIV1 element.&lt;/p&gt;

&lt;p&gt;Then the &lt;code&gt;componentDidUpdate()&lt;/code&gt; method is executed and writes a message in the empty DIV2 element:&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;Use the &lt;code&gt;getSnapshotBeforeUpdate()&lt;/code&gt; method to find out what the &lt;code&gt;state&lt;/code&gt; object looked like before the update:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favoritecolor: "red"};
  }
  componentDidMount() {
    setTimeout(() =&amp;gt; {
      this.setState({favoritecolor: "yellow"})
    }, 1000)
  }
  getSnapshotBeforeUpdate(prevProps, prevState) {
    document.getElementById("div1").innerHTML =
    "Before the update, the favorite was " + prevState.favoritecolor;
  }
  componentDidUpdate() {
    document.getElementById("div2").innerHTML =
    "The updated favorite is " + this.state.favoritecolor;
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;My Favorite Color is {this.state.favoritecolor}&amp;lt;/h1&amp;gt;
        &amp;lt;div id="div1"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div id="div2"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  5. componentDidUpdate
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;componentDidUpdate&lt;/code&gt; method is called after the component is updated in the DOM.&lt;/p&gt;

&lt;p&gt;The example below might seem complicated, but all it does is this:&lt;/p&gt;

&lt;p&gt;When the component is mounting it is rendered with the favorite color "red".&lt;/p&gt;

&lt;p&gt;When the component has been mounted, a timer changes the state, and the color becomes "yellow".&lt;/p&gt;

&lt;p&gt;This action triggers the update phase, and since this component has a &lt;code&gt;componentDidUpdate&lt;/code&gt; method, this method is executed and writes a message in the empty DIV element:&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;componentDidUpdate&lt;/code&gt; method is called after the update has been rendered in the DOM:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {favoritecolor: "red"};
  }
  componentDidMount() {
    setTimeout(() =&amp;gt; {
      this.setState({favoritecolor: "yellow"})
    }, 1000)
  }
  componentDidUpdate() {
    document.getElementById("mydiv").innerHTML =
    "The updated favorite is " + this.state.favoritecolor;
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;My Favorite Color is {this.state.favoritecolor}&amp;lt;/h1&amp;gt;
      &amp;lt;div id="mydiv"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Header /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Unmounting Phase
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;unmounting phase&lt;/strong&gt; is the third and final phase of a React component. At this phase, the component is removed from the DOM. Unmounting only has one lifecycle method involved: &lt;code&gt;componentWillUnmount&lt;/code&gt;.&lt;/p&gt;




&lt;h4&gt;
  
  
  componentWillUnmount
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;componentWillUnmount&lt;/code&gt; is invoked right before the component is unmounted or removed from the DOM. It’s meant for any necessary clean up of the component, like unsubscribing to any subscriptions (i.e., Redux) or canceling any network requests. Once this method is done executing, the component will be destroyed.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;componentWillUnmount&lt;/code&gt; method is called when the component is about to be removed from the DOM.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Container extends React.Component {
  constructor(props) {
    super(props);
    this.state = {show: true};
  }
  delHeader = () =&amp;gt; {
    this.setState({show: false});
  }
  render() {
    let myheader;
    if (this.state.show) {
      myheader = &amp;lt;Child /&amp;gt;;
    };
    return (
      &amp;lt;div&amp;gt;
      {myheader}
      &amp;lt;button type="button" onClick={this.delHeader}&amp;gt;Delete Header&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

class Child extends React.Component {
  componentWillUnmount() {
    alert("The component named Header is about to be unmounted.");
  }
  render() {
    return (
      &amp;lt;h1&amp;gt;Hello World!&amp;lt;/h1&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Container /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In React, a component can enter three different phases that make up its lifecycle. These phases are mounting, updating, and unmounting. Each phase has lifecycle methods invoked, where you can work on different things related to the component, like its &lt;code&gt;props&lt;/code&gt; and &lt;code&gt;state&lt;/code&gt;, or actually mounting the component to the DOM (with the render method). However, these methods are only for class-based components.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactlifecycle</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
