Mini Dashboard app
A mini dashboard combines a layout (sidebar, header, main area), several widgets (KPI cards, a chart, a recent-activity table) that load data independently, filters stored in the URL, and good loading, empty and error states. A strong interview answer separates layout from widgets, fetches in parallel with TanStack Query (or route loaders), wraps each widget in its own Suspense or error boundary, and keeps the UI responsive with transitions.
Architecture
- Layout:
DashboardLayoutwith<Outlet />(React Router) or nested layouts (Next.js), responsive sidebar. - Filters in the URL: date range and team via
useSearchParams, so views are shareable. - Data per widget: each widget has its own query keyed by the filters (
["kpis", range],["revenue", range]), so they load in parallel and refetch independently. - Isolation: each widget inside its own error boundary and loading state, so one failing API does not break the page.
- Responsiveness:
useTransitionwhen changing filters keeps old data visible;placeholderData: keepPreviousDataavoids flashing skeletons. - Charts: a charting library (Recharts, visx, ECharts), lazy-loaded since chart code is heavy.
- Tables: sorting and pagination; virtualization for large datasets.
- Accessibility: semantic landmarks (
header,nav,main), table headers, chart summaries in text.
Performance
Code split heavy widgets, cache with sensible staleTime, refetch on focus for fresh numbers, and enable the React Compiler.
Extensions
Real-time updates via WebSocket that update the query cache, saved views, CSV export, dark mode, role-based widgets.
Code example
1import { lazy, Suspense, useTransition } from "react";2import { useSearchParams } from "react-router";3import { keepPreviousData, useQuery } from "@tanstack/react-query";4import { ErrorBoundary } from "react-error-boundary";56const RevenueChart = lazy(() => import("./revenue-chart")); // heavy chart code split out7type Range = "7d" | "30d" | "90d";89const getJson = <T,>(url: string) => fetch(url).then((r) => (r.ok ? (r.json() as Promise<T>) : Promise.reject(new Error(`HTTP ${r.status}`))));1011function Kpis({ range }: { range: Range }) {12 const { data, isPending } = useQuery({ queryKey: ["kpis", range], queryFn: () => getJson<{ label: string; value: string }[]>(`/api/kpis?range=${range}`), placeholderData: keepPreviousData });13 if (isPending) return <div className="h-20 animate-pulse bg-slate-100" />;14 return <div className="grid grid-cols-3 gap-4">{data!.map((k) => <article key={k.label}><p>{k.label}</p><strong>{k.value}</strong></article>)}</div>;15}1617function RecentOrders({ range }: { range: Range }) {18 const { data = [] } = useQuery({ queryKey: ["orders", range], queryFn: () => getJson<{ id: string; total: number }[]>(`/api/orders?range=${range}`) });19 return <table><thead><tr><th>Order</th><th>Total</th></tr></thead><tbody>{data.map((o) => <tr key={o.id}><td>{o.id}</td><td>${o.total}</td></tr>)}</tbody></table>;20}2122const Widget = ({ title, children }: { title: string; children: React.ReactNode }) => (23 <section aria-label={title}><h2>{title}</h2>24 <ErrorBoundary fallback={<p role="alert">{title} failed to load.</p>}><Suspense fallback={<p>Loading…</p>}>{children}</Suspense></ErrorBoundary>25 </section>26);2728export function Dashboard() {29 const [params, setParams] = useSearchParams();30 const range = (params.get("range") as Range) ?? "30d";31 const [isPending, startTransition] = useTransition();3233 return (34 <main style={{ opacity: isPending ? 0.7 : 1 }}>35 <header><h1>Dashboard</h1>36 <select value={range} onChange={(e) => startTransition(() => setParams({ range: e.target.value }))}>37 <option value="7d">Last 7 days</option><option value="30d">Last 30 days</option><option value="90d">Last 90 days</option>38 </select>39 </header>40 <Widget title="Key metrics"><Kpis range={range} /></Widget>41 <Widget title="Revenue"><RevenueChart range={range} /></Widget>42 <Widget title="Recent orders"><RecentOrders range={range} /></Widget>43 </main>44 );45}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.