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 dev

Project 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 config

What 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

HTMLJSXWhy
classclassNameclass is a reserved JS keyword
forhtmlForfor is a reserved JS keyword
onclickonClickcamelCase event names
style="color: red"style={{ color: 'red' }}Object, not string
tabindextabIndexcamelCase attributes
Self-closing optionalSelf-closing required<img />, <br />, <input />

JSX Rules

  1. Return a single root element β€” use <div> or <> (Fragment)
  2. Close all tags β€” <img />, <input />, <br />
  3. camelCase for most attributes
  4. Expressions in {} β€” any JS expression: {2 + 2}, {user.name}, {getTitle()}
  5. No if statements 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