SkillPilotAI

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.
JavaScript
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);
Output
-- before --Cart: 0Total: ₹0 -- after two addToCart calls --Cart: 2Total: ₹0

23.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.
Imperative update path
  1. 1.Data changes (count, total)

  2. 2.Find every element by hand (querySelector)

  3. 3.Set each one (textContent)

  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.
PointImperative (vanilla JS)Declarative (React)
You writeEvery DOM stepWhat the screen should show
DOM updatesBy hand (querySelector, textContent)Done by React
Data shown in many placesUpdate each place yourselfRe-render keeps all in sync
Typical bugForgot to update one elementRare: 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.
React (TSX)
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>  );}
Output
Cart: 2Total: ₹798

23.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.
How an SPA works
  1. 1.Browser loads one HTML page: index.html + JS bundle

  2. 2.React renders the UI inside div#root

  3. 3.User clicks a link

  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.
Terminal
npm create vite@latest cart-app -- --template react-tscd cart-appnpm installnpm run dev
Output
$ 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

FileRole
index.htmlHas one empty div with id root
src/main.tsxEntry point: hands div#root to React
src/App.tsxThe first component
package.jsonScripts and dependencies
vite.config.tsVite 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.
React (TSX)
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).
React (TSX)
import { Cart } from "./Cart"; export default function App() {  return (    <main>      <h1>Seekho Store</h1>      <Cart prices={[499, 299]} />    </main>  );}
Output
# Seekho StoreCart: 2Total: ₹798

Checkpoint: 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.

React (TSX)
export default function App() {  console.log("App render");  return <h1>Seekho Store</h1>;}
Output
App renderApp render Screen: dev 2 times, production 1 time

23.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.
React (TSX)
// 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 />);
React (TSX)
// Right: same id, App renders// index.html: <div id="root"></div>const el = document.getElementById("root"); createRoot(el!).render(<App />);

23.11 Setup rules

  1. Create projects with the Vite react-ts template, not Create React App.
  2. Keep StrictMode; it catches bugs early.
  3. Name component files with a capital letter, like App.tsx.
  4. 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.