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 a Senior Full Stack Engineer: React, Next.js 15, TypeScript & Real-Time PWAs
- Hire a Senior Cloud Engineer: Cloud Architecture Blueprints
⚡ 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) →