TypeScript के साथ React Dashboard
In this page:
interface ComponentProps {
propName: type;
}
function ComponentName({ propName }: ComponentProps): React.JSX.Element {
return <div>{propName}</div>;
}
Core Concept
एक typed React dashboard में typically कई data-driven widgets होते हैं (charts, tables, stat cards), हर एक का अपना typed props interface जो exactly describe करता है कि यह अपने parent से क्या data और callbacks expect करता है।
उदाहरण: Core Concept
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
एक basic setup create-vite@latest -- --template react-ts या similar से scaffold करता है, typed .tsx components और एक tsconfig.json देते हुए जो JSX के लिए already configured है।
उदाहरण: Basic Setup
// npm create vite@latest dashboard -- --template react-ts
console.log("Scaffolds typed .tsx components with tsconfig.json ready for JSX");
Typed Example
एक typed example: interface ChartWidgetProps { data: DataPoint[]; onPointClick?: (point: DataPoint) => void } ensure करता है कि widget जहां भी उपयोग हो वहां correctly-shaped data और, अगर दिया जाए, एक correctly-typed click handler pass हो।
उदाहरण: Typed Example
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
एक असली project में, हर widget को feed करने वाले API responses को type करना (एक shared interface DashboardStats के ज़रिए) का मतलब है कि एक backend field rename तुरंत dashboard में एक compile error के रूप में दिखता है, silently undefined render होने के बजाय।
उदाहरण: Project Usage
interface DashboardStats {
activeUsers: number;
revenue: number;
}
const stats: DashboardStats = { activeUsers: 120, revenue: 4500 };
console.log(stats);
Best Practices
अपने custom hooks के return values को explicitly type करें (जैसे useDashboardData(): { stats: DashboardStats | null; loading: boolean }) इसलिए हर consuming component को उस data पर full autocomplete और null-safety मिले जिस पर यह depend करता है।
उदाहरण: Best Practices
interface DashboardStats {
activeUsers: number;
}
function useDashboardData(): { stats: DashboardStats | null; loading: boolean } {
return { stats: null, loading: true };
}
console.log(useDashboardData());
- Widget props को loosely type करना, जैसे
data: any, इसलिए एक गलत data shape catch नहीं होता। - Callback props को required बनाना जब वे optional हैं, या optional callbacks को
?.check के बिना call करना। - यह मानना कि API data
DashboardStatsinterface से match करता है, जब असली response अलग हो सकता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Todo App with TypeScript
- REST API with Express + TypeScript
- React Dashboard with TypeScript
- CLI Tool with TypeScript
- Library with TypeScript
- Full Stack TypeScript App
- TypeScript Design System
- TypeScript Monorepo Project
- Authentication System
- Real-time App with Socket.io
- GraphQL API with TypeScript
- Microservices with TypeScript
- TypeScript Best Practices Review
- What to Learn Next