TypeScript के साथ React का उपयोग
interface ComponentProps {
name: string;
count?: number;
}
function Component({ name, count = 0 }: ComponentProps) {
const [value, setValue] = useState<string>("");
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => setValue(e.target.value);
return <input value={value} onChange={handleChange} />;
}
Component Props को Type करना
एक interface (या type alias) define करना जो किसी component की expected props describe करे, फिर उसे props parameter के type के रूप में उपयोग करना, TypeScript को एक missing required prop या wrong type की value जैसी mistakes को compile time पर, code run होने से पहले ही पकड़ने देता है।
उदाहरण: Typing Component Props
// Run in your local React project (npm install required)
interface GreetingProps {
name: string;
age?: number; // optional
}
function Greeting({ name, age }: GreetingProps) {
return <p>Hello, {name}{age ? ` (${age})` : ''}</p>;
}
useState को Type करना
TypeScript आमतौर पर एक useState hook का type उसकी initial value से automatically infer करता है, जैसे useState(0) number के रूप में infer होता है। जब initial value intended type को पूरी तरह capture नहीं करती (जैसे एक empty array जो बाद में strings रखेगा), आप explicitly एक generic के साथ type specify करते हैं।
उदाहरण: Typing useState
// Run in your local React project (npm install required)
function TodoList() {
const [todos, setTodos] = useState<string[]>([]);
const addTodo = (text: string) => setTodos([...todos, text]);
return <button onClick={() => addTodo('New task')}>Add ({todos.length})</button>;
}
Event Handlers को Type करना
React के built-in TypeScript types में specific event types शामिल हैं, जैसे एक input के onChange handler के लिए React.ChangeEvent<HTMLInputElement>, आपको event.target.value जैसी properties पर correct autocomplete और type-checking देते हुए।
उदाहरण: Typing Event Handlers
// Run in your local React project (npm install required)
function SearchBox() {
const [query, setQuery] = useState('');
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setQuery(e.target.value);
};
return <input value={query} onChange={handleChange} />;
}
- एक component की props को any type करना, जो TypeScript जोड़ने के पूरे purpose को defeat करता है।
- कुछ TS configurations में JSX.Element या React.FC return types के लिए ज़रूरी React import भूल जाना।
- useState का generic type न करना जब initial value पूरे type को clearly imply नहीं करती (जैसे एक list को empty array से शुरू करना)।
- TypeScript React में static type-checking जोड़ता है, कई prop/state mismatches runtime से पहले पकड़ते हुए।
- एक component की props typically इसकी props object के लिए एक interface या type alias से typed होती हैं।
- useState<Type>(initialValue) आपको explicitly एक hook का type specify करने देता है जब यह correctly infer नहीं हो सकता।
- TypeScript सीधे React के existing patterns में integrate होता है — components, hooks, और props सब typed हो जाते हैं, replace नहीं होते।
एक TypeScript-aware build setup चाहिए (Vite/Next.js templates इसे out of the box support करते हैं); किसी भी React version के साथ काम करता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Error Boundaries
- React Portals
- Modals using Portals (practical use)
- React Suspense
- Code Splitting with React.lazy
- Introduction to Server Components
- Introduction to Next.js (server-side React)
- Using React with TypeScript
- Scalable Folder Architecture
- Common React Design Patterns
- Component Documentation with Storybook
- Accessibility (a11y) in React
- i18n with react-i18next
- React Security Best Practices