Full Stack Engineer Guide: Rendering Interactive Mermaid.js Architecture Diagrams in React, Next.js & Technical Portals

Clear architecture visualization—sequence diagrams for OAuth2/webhook flows, state diagrams for order lifecycles, and entity-relationship diagrams for PostgreSQL schemas—dramatically accelerates engineering alignment. As a Senior Full Stack Engineer, I integrate Mermaid.js directly into React/Next.js applications and AI documentation portals so diagrams remain version-controlled as code rather than stale raster images.

1. Safe Asynchronous Mermaid SVG Rendering in React & TypeScript

When rendering dynamic Mermaid definitions generated by users or AI agents, a production React component must sanitize output, isolate unique DOM IDs, and handle syntax errors gracefully without crashing the page tree:

import React, { useEffect, useId, useState } from "react";
import mermaid from "mermaid";

mermaid.initialize({
  startOnLoad: false,
  securityLevel: "strict",
  theme: "neutral",
});

export function MermaidDiagram({ chart }: { chart: string }) {
  const reactId = useId().replace(/:/g, "");
  const [svgHtml, setSvgHtml] = useState<string>("");
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    let cancelled = false;
    async function renderChart() {
      try {
        const { svg } = await mermaid.render(`mermaid-${reactId}`, chart);
        if (!cancelled) {
          setSvgHtml(svg);
          setError(null);
        }
      } catch (err) {
        if (!cancelled) {
          setError(err instanceof Error ? err.message : "Invalid diagram syntax");
        }
      }
    }
    renderChart();
    return () => {
      cancelled = true;
    };
  }, [chart, reactId]);

  if (error) {
    return <pre style={{ color: "#b91c1c" }}>{error}</pre>;
  }
  return <div dangerouslySetInnerHTML={{ __html: svgHtml }} />;
}

2. Related Full Stack & Cloud Architecture Guides


⚡ Hire Aldian Fazrihady — Senior Cloud, GCP, Backend, AI Application & Full Stack Engineer

Need production cloud architecture, GCP/AWS infrastructure, high-throughput backend systems, AI agents, or full stack web engineering? Book a 1-Day Programming Sprint for $100 USD (International Card / Apple Pay) or Rp 1,000,000 (Indonesian QRIS / Bank Virtual Account), or hire directly on Upwork:

Book $100 / Rp 1M 1-Day Sprint → Hire on Upwork (100% Job Success) →


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 15+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs