Week 4 - React Fundamentals

Why React?

Setting up a React project with Vite

Components

JSX

Props

Rendering lists

Conditional rendering

The mental model: how React renders (2)

Practice

Assignment

Front end Track

Setting Up a React Project with Vite

Before you can write a single component, your code needs to go through some transformation. JSX isn't valid JavaScript โ€” the browser can't read it. import and export statements need to be bundled into files the browser can load. Modern syntax sometimes needs to be converted for older browsers.

A build tool handles all of this for you. You write modern React code, it produces something the browser understands.

Why Vite?

For years the standard was Create React App (CRA). You'll still find it in older tutorials and codebases. CRA is now legacy โ€” slow to start, slow to rebuild, and no longer maintained. The current standard is Vite (French for "fast," pronounced veet).

https://www.youtube.com/watch?v=KCrXgy8qtjM

Vite gives you:

<aside> ๐Ÿ’ก

Info

Under the hood, Vite uses native ES modules in the browser during development, which is why it starts so fast. For production builds it uses Rollup. You don't need to memorize this โ€” Vite is a friendly wrapper around those tools plus a dev server. Just know that "fast" is the point.

</aside>

Creating Your First Project

Run this in your terminal:

npm create vite@latest my-first-app -- --template react-ts
cd my-first-app
npm install
npm run dev

Vite will print a local URL โ€” usually http://localhost:5173. Open it. You should see a running React app.

<aside> โš ๏ธ

Warning

Don't forget the cd my-first-app step before running npm install. This is the most common stumbling block โ€” you'll install dependencies in the wrong folder and nothing will work.

</aside>

The Folder Structure

Once the project is created, this is what you'll find:

my-first-app/
โ”œโ”€โ”€ node_modules/
โ”œโ”€โ”€ public/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ assets/
โ”‚   โ”œโ”€โ”€ App.tsx
โ”‚   โ”œโ”€โ”€ App.css
โ”‚   โ”œโ”€โ”€ main.tsx
โ”‚   โ””โ”€โ”€ index.css
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tsconfig.json
โ”œโ”€โ”€ vite.config.ts
โ””โ”€โ”€ .gitignore

A quick tour of what matters:

Open index.html. You'll find exactly one meaningful element:

<div id="root"></div>

That's it. Your entire application will render inside that one <div>. The rest of the HTML is boilerplate โ€” the script tag at the bottom tells the browser to load your bundled JavaScript.

The Entry Point

Open src/main.tsx:

import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
import './index.css';

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <App />
  </StrictMode>
);

This file connects React to the DOM. createRoot finds the <div id="root"> element and hands it to React. From that point on, React owns that node โ€” it renders your component tree inside it and keeps the DOM up to date.

<StrictMode> is a development-only helper. It doesn't affect the production build, but in development it warns you about common mistakes and deprecated patterns. Leave it in.

<aside> ๐Ÿ’ก

Info

The ! after document.getElementById('root') is TypeScript telling the compiler "trust me, this element exists." Without it, TypeScript would warn that getElementById might return null. You'll see this pattern in a few other places โ€” it's called a non-null assertion.

</aside>

npm Scripts

Open package.json and look at the scripts section:

{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  }
}

<aside> โŒจ๏ธ

Hands On

  1. Open src/App.tsx and change some text โ€” anything you like.
  2. Save the file and watch the browser update instantly without a manual refresh. That's HMR.
  3. Stop the server (Ctrl+C), run npm run build, and look inside the generated /dist folder. Those are the files you'd upload to a hosting provider.
  4. Run npm run preview to serve those files locally and see what your users will see. </aside>

What You Just Set Up

Your project is a single HTML page with one empty <div>. React takes over that <div> and renders everything from JavaScript. The browser never navigates to a new page โ€” React swaps components in and out as the user interacts.

This is called a Single Page Application (SPA). It's the foundation of how React apps are structured, and it's why routing (which you'll cover in week 9) has to work differently than traditional server-rendered sites.

<aside> ๐ŸŽ‰

Celebration

You have a working React project running locally. That's your development environment sorted. From here, everything you learn goes into src/ โ€” and Vite handles the rest.

</aside>

Additional Resources

Videos

Reading


The HackYourFuture curriculum is licensed underย CC BY-NC-SA 4.0 *https://hackyourfuture.net/*

CC BY-NC-SA 4.0 Icons

Built with โค๏ธ by the HackYourFuture community ยท Thank you, contributors

Found a mistake or have a suggestion? Let us know in the feedback form.