← Back to TypeScript Course | Chapter 28: Real World Projects | Lesson 3 of 14

React Dashboard with TypeScript

React and TypeScript work together to type component props, state, events, and reusable UI models. A dashboard is a useful example of these patterns.

Core Concept

A typed React dashboard typically has several data-driven widgets (charts, tables, stat cards), each with its own typed props interface describing exactly what data and callbacks it expects from its parent.

Example: Core Concept

typescript
import React from "react";

interface StatCardProps {
	label: string;
	value: number;
}
function StatCard({ label, value }: StatCardProps): React.JSX.Element {
	return <div>{label}: {value}</div>;
}
export default StatCard;

Basic Setup

A basic setup scaffolds with create-vite@latest -- --template react-ts or similar, giving typed .tsx components and a tsconfig.json already configured for JSX out of the box.

Example: Basic Setup

typescript
// npm create vite@latest dashboard -- --template react-ts
console.log("Scaffolds typed .tsx components with tsconfig.json ready for JSX");

Typed Example

A typed example: interface ChartWidgetProps { data: DataPoint[]; onPointClick?: (point: DataPoint) => void } ensures every place the widget is used passes correctly-shaped data and, if provided, a correctly-typed click handler.

Example: Typed Example

typescript
import React from "react";

interface DataPoint {
	x: number;
	y: number;
}
interface ChartWidgetProps {
	data: DataPoint[];
	onPointClick?: (point: DataPoint) => void;
}
function ChartWidget({ data, onPointClick }: ChartWidgetProps): React.JSX.Element {
	return <div>{data.length} points</div>;
}
export default ChartWidget;

Project Usage

In a real project, typing the API responses that feed each widget (via a shared interface DashboardStats) means a backend field rename shows up as a compile error in the dashboard immediately, instead of silently rendering undefined.

Example: Project Usage

typescript
interface DashboardStats {
	activeUsers: number;
	revenue: number;
}
const stats: DashboardStats = { activeUsers: 120, revenue: 4500 };
console.log(stats);

Best Practices

Type your custom hooks' return values explicitly (e.g. useDashboardData(): { stats: DashboardStats | null; loading: boolean }) so every consuming component gets full autocomplete and null-safety on the data it depends on.

Example: Best Practices

typescript
interface DashboardStats {
	activeUsers: number;
}
function useDashboardData(): { stats: DashboardStats | null; loading: boolean } {
	return { stats: null, loading: true };
}
console.log(useDashboardData());

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.