2. Other Create React App Options
2. Other Create React App Options
1. Create a New React App
Using Vite
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
npm run dev
Using Create React App (Recommended)
npx create-react-app my-app --template typescript
cd my-app
npm start
2. Project Structure
my-app/
├── src/
│ ├── App.tsx # Main component
│ ├── main.tsx # Entry point
│ ├── index.css # Global styles
│ └── vite-env.d.ts # Vite type declarations
├── public/
├── index.html
├── package.json
├── tsconfig.json # TypeScript config
└── vite.config.ts # Vite config
3. Create a Component
// src/components/Button.tsx
interface ButtonProps {
label: string;
onClick: () => void;
disabled?: boolean;
}
const Button = ({ label, onClick, disabled = false }: ButtonProps) => {
return (
<button onClick={onClick} disabled={disabled}>
{label}
</button>
);
};
export default Button;
4. Use State with TypeScript
import { useState } from 'react';
const Counter = () => {
const [count, setCount] = useState<number>(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
};
5. Use Props with Children
interface CardProps {
title: string;
children: React.ReactNode;
}
const Card = ({ title, children }: CardProps) => {
return (
<div className="card">
<h2>{title}</h2>
{children}
</div>
);
};
6. Handle Events
const Form = () => {
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
// Handle form submission
};
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
console.log(e.target.value);
};
return (
<form onSubmit={handleSubmit}>
<input type="text" onChange={handleChange} />
<button type="submit">Submit</button>
</form>
);
};
7. Use useEffect
import { useEffect, useState } from 'react';
interface User {
id: number;
name: string;
}
const UserList = () => {
const [users, setUsers] = useState<User[]>([]);
useEffect(() => {
fetch('/api/users')
.then(res => res.json())
.then(data => setUsers(data));
}, []);
return (
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
};
8. Use useRef
import { useRef } from 'react';
const InputFocus = () => {
const inputRef = useRef<HTMLInputElement>(null);
const focusInput = () => {
inputRef.current?.focus();
};
return (
<>
<input ref={inputRef} type="text" />
<button onClick={focusInput}>Focus Input</button>
</>
);
};
9. Create Custom Hooks
import { useState, useEffect } from 'react';
const useLocalStorage = <T,>(key: string, initialValue: T) => {
const [value, setValue] = useState<T>(() => {
const stored = localStorage.getItem(key);
return stored ? JSON.parse(stored) : initialValue;
});
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);
return [value, setValue] as const;
};
10. Add Routing
npm install react-router-dom
import { BrowserRouter, Routes, Route } from 'react-router-dom';
const App = () => {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/users/:id" element={<UserDetail />} />
</Routes>
</BrowserRouter>
);
};
11. Build for Production
npm run build
Output will be in the dist/ folder (Vite) or build/ folder (CRA).
Common TypeScript Types for React
| Type | Usage |
|---|---|
React.FC<Props> |
Function component type |
React.ReactNode |
Any renderable content |
React.ReactElement |
A React element |
React.ChangeEvent<HTMLInputElement> |
Input change event |
React.FormEvent<HTMLFormElement> |
Form submit event |
React.MouseEvent<HTMLButtonElement> |
Button click event |