Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 2 min read

How to Implement WhatsApp Click-to-Chat QR Codes with SVG in Web Apps

In modern mobile-first web development, reducing conversion friction on landing pages is the fastest lever to increase inbound lead velocity. While desktop users can click on an inline WhatsApp link to open WhatsApp Web,

In modern mobile-first web development, reducing conversion friction on landing pages is the fastest lever to increase inbound lead velocity. While desktop users can click on an inline WhatsApp link to open WhatsApp Web, mobile and cross-device visitors benefit significantly from dynamic, scannable QR codes rendered directly on screen.

Unlike raster image formats (PNG, JPEG) which blur upon zooming or increase HTTP payload sizes, generating vector SVG QR codes client-side ensures crisp rendering on retina displays with zero external image requests.

In this tutorial, we will build a lightweight, responsive WhatsApp QR code component in React and TypeScript that formats dynamic click-to-chat deep links.

Understanding the WhatsApp Deep Link Protocol

WhatsApp supports universal deep links formatted as:

https://wa.me/<PhoneNumber>?text=<EncodedMessage>

Key formatting requirements:

  1. E.164 Format: The phone number must include the international country code without plus signs, dashes, or leading zeros (e.g., 15551234567).
  2. URL Encoding: The message string must be properly URI-encoded using encodeURIComponent to handle emojis, spaces, and punctuation.

Step 1: Installing the Lightweight QR Code Generator

We will use qrcode which supports zero-dependency SVG string generation:

npm install qrcode
npm install --save-dev @types/qrcode

Step 2: Creating the Responsive React QR Component

Here is the complete implementation of our WhatsApp QR component:

import React, { useMemo } from 'react';
import QRCode from 'qrcode';

interface WhatsAppQRProps {
  phoneNumber: string; // e.g. "15551234567"
  defaultMessage: string;
  size?: number;
}

export const WhatsAppQRCode: React.FC<WhatsAppQRProps> = ({
  phoneNumber,
  defaultMessage,
  size = 220
}) => {
  const cleanPhone = phoneNumber.replace(/[^0-9]/g, '');
  const encodedText = encodeURIComponent(defaultMessage);
  const waUrl = `https://wa.me/${cleanPhone}?text=${encodedText}`;

  const [svgString, setSvgString] = React.useState<string>('');

  React.useEffect(() => {
    QRCode.toString(waUrl, {
      type: 'svg',
      margin: 2,
      color: {
        dark: '#128C7E', // WhatsApp Brand Green
        light: '#FFFFFF'
      }
    }, (err, svg) => {
      if (!err && svg) setSvgString(svg);
    });
  }, [waUrl]);

  return (
    <div style={{ display: 'inline-block', textAlign: 'center' }}>
      <div 
        dangerouslySetInnerHTML={{ __html: svgString }} 
        style={{ width: size, height: size, margin: '0 auto' }} 
      />
      <a 
        href={waUrl} 
        target="_blank" 
        rel="noopener noreferrer"
        style={{
          display: 'inline-block',
          marginTop: 12,
          padding: '8px 16px',
          backgroundColor: '#25D366',
          color: '#fff',
          borderRadius: 8,
          textDecoration: 'none',
          fontWeight: 600
        }}
      >
        Chat on WhatsApp
      </a>
    </div>
  );
};

Enhancing Conversion Tracking and Lead Routing

To measure campaign effectiveness, avoid hardcoding static numbers in your production client bundle. Instead:

  • Use UTM campaign tracking parameters within the pre-filled message string so your CRM can attribute inbound chats to specific ads or blog posts.
  • For non-technical marketing teams, you can generate instant, trackable QR links using the Free WhatsApp Link Generator on AI Soul.
  • Once prospects scan the code and start chatting, route them into a multi-agent shared workspace with Conversational CRM Features on AI Soul and deploy automated AI Chatbot Solutions to qualify inquiries immediately.

Summary

  • Generate QR codes client-side in SVG format for zero layout shift and retina crispness.
  • Sanitize phone numbers to pure numeric E.164 strings.
  • Pair QR codes with direct one-tap links to serve both desktop and mobile users seamlessly.
πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.