A modern, production-ready reference implementation demonstrating how to cleanly integrate react-dropzone with Next.js 16 App Router, TypeScript, and Tailwind CSS.
Using react-dropzone in modern Next.js requires understanding how client-side file APIs interact with Server-Side Rendering (SSR) and the App Router:
-
Client Boundary (
'use client'):react-dropzonerelies on DOM event listeners,useRef,window, anddocumentdrag-and-drop APIs. Wrapping dropzone logic inside a'use client'component creates a clean boundary while allowing root routes (page.tsx) to remain fast, SEO-friendly Server Components. -
Hydration Safety: Dropzone input IDs and browser-only properties should not cause server/client HTML divergence. Files are only accessed client-side inside
onDropcallbacks, completely preventing React hydration mismatches. -
Memory Leak Prevention: When rendering client-side image thumbnails via
URL.createObjectURL(file), the generated blob URLs must be explicitly revoked viaURL.revokeObjectURL(url)during component unmount or when files are removed. -
Handling Uploads with Server Actions / API Routes: Dropped
Fileobjects can be packaged into standardFormDataand streamed to Next.js Route Handlers (app/api/upload/route.ts) or Server Actions.
- TypeScript 5: Fully type-safe props, event handlers, and extended file preview interfaces.
- Dynamic Drag States: Clear visual feedback for active dragging, accepted file types (
isDragAccept), and rejected formats (isDragReject). - Rich File Previews: Image thumbnail rendering with automatic cleanup, plus distinct file icons for PDFs, code, audio/video, and archives.
- Validation & Error Handling: User-friendly alerts displaying rejection reasons (max size exceeded, format disallowed, too many files).
- Multiple Dropzone Variants:
- Multi-File Dropzone: Configurable file count, size limits, and removable file grid.
- Avatar / Single Image Uploader: Circular profile photo uploader with hover-to-replace overlay.
- Server Upload Simulation: Interactive upload progress bar and file metadata summary.
- Tailwind CSS & Dark Mode: Responsive design with seamless dark/light theme support.
react-dropzone-nextjs/
├── src/
│ ├── app/
│ │ ├── layout.tsx # Root layout with Geist font & SEO metadata
│ │ ├── page.tsx # Server Component entry point
│ │ └── globals.css # Tailwind styling & dark mode tokens
│ ├── components/
│ │ ├── dropzone.tsx # Reusable, feature-complete Multi-File Dropzone
│ │ ├── avatar-dropzone.tsx# Single image/avatar dropzone
│ │ ├── showcase.tsx # Tabbed demo manager with live settings
│ │ └── ssr-guide.tsx # In-app architectural guide for SSR & Dropzone
│ ├── lib/
│ │ └── utils.ts # Class merging (cn) & human-readable formatBytes
│ └── types/
│ └── dropzone.ts # TypeScript definitions
├── tsconfig.json
└── package.json
npm installnpm run devOpen http://localhost:3000 with your browser to view the interactive showcase.
# Type check
npm run type-check
# Lint
npm run lint
# Test
npm test
# Production build
npm run build"use client";
import { ModernDropzone } from "@/components/dropzone";
export function UploadSection() {
return (
<ModernDropzone
maxFiles={5}
maxSize={10 * 1024 * 1024} // 10 MB
accept={{ "image/*": [".png", ".jpg", ".webp"] }}
onFilesAccepted={(files) => console.log("Accepted files:", files)}
onFilesRejected={(rejections) => console.warn("Rejected files:", rejections)}
/>
);
}"use client";
async function uploadFiles(files: File[]) {
const formData = new FormData();
files.forEach((file) => formData.append("files", file));
const response = await fetch("/api/upload", {
method: "POST",
body: formData,
});
return response.json();
}MIT