What is React and Vite setup
React is a declarative JavaScript library from Meta for building user interfaces: you describe what the screen should look like for the current data (UI = f(state)) and React updates the DOM for you. This module shows why that removes a whole class of bugs and sets up a first React + TypeScript project with Vite.
23.1 The problem: you do every update yourself
- A cart without React: count and total variables, plus two page elements.
- addToCart updates both variables but only changes the badge text by hand.
- After two items the badge shows Cart: 2, but Total stays ₹0 because we forgot to update #sum.
let count = 0;let total = 0;const badge = document.querySelector("#cart");const sumEl = document.querySelector("#sum"); function addToCart(price) { count = count + 1; total = total + price; badge.textContent = `Cart: ${count}`;} addToCart(499);addToCart(299);-- before --Cart: 0Total: ₹0 -- after two addToCart calls --Cart: 2Total: ₹023.2 Why imperative code has bugs
- Imperative style: you tell the browser every single step (querySelector + textContent).
- In a real app the same data appears in many places on the screen.
- Forget one place and data and screen go out of sync: the biggest source of UI bugs.
- Step 1
1.Data changes (count, total)
- Step 2
2.Find every element by hand (querySelector)
- Step 3
3.Set each one (textContent)
- Step 4
4.Forget one element: screen out of sync
23.3 React's idea: declarative UI
- React is a library for building UIs, made by Meta.
- Like booking an Ola or Uber: you give only the destination, not every turn.
- Declarative: describe how the screen should look for this data; DOM updates are React's job.
- UI = f(state): the screen is just the result of the data.
| Point | Imperative (vanilla JS) | Declarative (React) |
|---|---|---|
| You write | Every DOM step | What the screen should show |
| DOM updates | By hand (querySelector, textContent) | Done by React |
| Data shown in many places | Update each place yourself | Re-render keeps all in sync |
| Typical bug | Forgot to update one element | Rare: screen always derived from data |
23.4 The same cart in React
- Cart is a normal function that takes an array of prices.
- reduce calculates the total; the return only describes the screen.
- No querySelector or textContent anywhere; the HTML-like syntax is JSX.
- When data changes React calls the function again, so the total always matches.
type CartProps = { prices: number[] }; export function Cart({ prices }: CartProps) { const total = prices.reduce((s, p) => s + p, 0); return ( <div> <p>Cart: {prices.length}</p> <p>Total: ₹{total}</p> </div> );}Cart: 2Total: ₹79823.5 SPA: single page application
- Older sites: every link click fetched a new HTML page and reloaded everything.
- SPA: the browser loads HTML + JavaScript once, then React changes content inside the page (feels like Gmail).
- Downside: slower first load and weaker SEO, solved later with Next.js.
- Step 1
1.Browser loads one HTML page: index.html + JS bundle
- Step 2
2.React renders the UI inside div#root
- Step 3
3.User clicks a link
- Step 4
4.No reload: React swaps only the content
Next.js later adds server rendering for first load and SEO
23.6 First project with Vite
- Vite is a fast build tool; Create React App is deprecated.
- Template react-ts = React with TypeScript.
- npm install fetches packages, npm run dev starts the dev server at localhost:5173.
npm create vite@latest cart-app -- --template react-tscd cart-appnpm installnpm run dev$ npm run dev> cart-app@0.0.0 dev> viteVITE v8.3.6 ready in 2457 ms➜ Local: http://localhost:5173/23.7 Project structure
| File | Role |
|---|---|
| index.html | Has one empty div with id root |
| src/main.tsx | Entry point: hands div#root to React |
| src/App.tsx | The first component |
| package.json | Scripts and dependencies |
| vite.config.ts | Vite settings |
23.8 main.tsx: where React starts
- Imports StrictMode, createRoot, the CSS file and App.
- createRoot(document.getElementById("root")!) hands the root div to React.
- The ! tells TypeScript the element is not null.
- render wraps App in StrictMode and shows it on screen.
import { StrictMode } from "react";import { createRoot } from "react-dom/client";import "./index.css";import App from "./App.tsx"; createRoot(document.getElementById("root")!).render( <StrictMode> <App /> </StrictMode>,);23.9 App.tsx: the first screen
- App returns a heading and the Cart with prices 499 and 299.
- Save a change and the browser updates without a reload: hot module replacement (HMR).
import { Cart } from "./Cart"; export default function App() { return ( <main> <h1>Seekho Store</h1> <Cart prices={[499, 299]} /> </main> );}# Seekho StoreCart: 2Total: ₹798Checkpoint: how many times does "App render" print in development? Twice: StrictMode renders every component twice on purpose in dev to catch impure code. In production it renders once, so do not remove StrictMode.
export default function App() { console.log("App render"); return <h1>Seekho Store</h1>;}App renderApp render Screen: dev 2 times, production 1 time23.10 Common mistakes
- getElementById("app") while index.html has id root: el is null, blank screen, console error Target container is not a DOM element. The ! hides it from TypeScript.
- Thinking React is a full framework: it only handles UI; routing and data fetching come from React Router, TanStack Query, or a framework like Next.js.
- Removing StrictMode because of the double log.
// Wrong: blank screen, "Target container is not a DOM element"// index.html: <div id="root"></div>const el = document.getElementById("app"); createRoot(el!).render(<App />);// Right: same id, App renders// index.html: <div id="root"></div>const el = document.getElementById("root"); createRoot(el!).render(<App />);23.11 Setup rules
- Create projects with the Vite react-ts template, not Create React App.
- Keep StrictMode; it catches bugs early.
- Name component files with a capital letter, like App.tsx.
- For production run npm run build, which writes optimized files to dist.
23.12 Interview tips
- React is a declarative JavaScript library for building UIs where UI = f(state).
- You describe the screen; when state changes React updates the DOM itself.
- React is a library, not a framework; Next.js is a framework built on React.
- React is usually used for SPAs: one HTML page whose content React changes.
- StrictMode double-renders only in development to expose impure code.