HomeReact
Chapter 1
01 β React Setup & JSX
Getting started with React using Vite (the modern build tool) and understanding JSX β the syntax extension that lets you write HTML-like code in JavaScript/TypeScript.
Quick Setup
# Create a new React + TypeScript project
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
npm run devProject Structure (Vite + React)
my-app/
βββ public/ # Static assets (served as-is)
βββ src/
β βββ App.tsx # Root component
β βββ main.tsx # Entry point (renders App into DOM)
β βββ index.css # Global styles
β βββ vite-env.d.ts # Vite type declarations
βββ index.html # HTML shell (Vite entry)
βββ package.json
βββ tsconfig.json # TypeScript config
βββ vite.config.ts # Vite configWhat is JSX?
JSX is a syntax extension that looks like HTML but compiles to JavaScript function calls:
// This JSX:
const element = <h1 className="title">Hello</h1>;
// Compiles to:
const element = React.createElement('h1', { className: 'title' }, 'Hello');JSX vs HTML Differences
| HTML | JSX | Why |
|---|---|---|
class | className | class is a reserved JS keyword |
for | htmlFor | for is a reserved JS keyword |
onclick | onClick | camelCase event names |
style="color: red" | style={{ color: 'red' }} | Object, not string |
tabindex | tabIndex | camelCase attributes |
| Self-closing optional | Self-closing required | <img />, <br />, <input /> |
JSX Rules
- Return a single root element β use
<div>or<>(Fragment) - Close all tags β
<img />,<input />,<br /> - camelCase for most attributes
- Expressions in
{}β any JS expression:{2 + 2},{user.name},{getTitle()} - No
ifstatements inside JSX β use ternary or&&
Key Concepts
- Fragment (
<>...</>) β group elements without adding a DOM node - Expressions β
{}can contain any JS expression, not statements - Comments β
{/* comment */}inside JSX - Boolean/null/undefined β not rendered (used for conditional rendering)
File
setup-jsx.tsxβ Vite setup walkthrough, JSX syntax, expressions, fragments, JSX vs HTML examples