Skip to main content
Albero Academy

Components & JSX

What React is, how to spin up a project, and the two ideas everything rests on — components and JSX.

Chapter 1React14 min readBeginner friendly
  • #React
  • #JSX
  • #Basics

React is the most widely-used library for building user interfaces — this very site is built with it. Its core idea is simple: describe your UI as a tree of small, reusable components, and let React keep the screen in sync with your data. This chapter assumes you're comfortable with the JavaScript course; if arrays, objects and arrow functions feel shaky, review those first.

Why React?

Without a library, keeping the DOM in sync with changing data means hand-writing dozens of querySelector updates that quickly tangle. React flips it around: you write what the UI should look like for the current data, and React figures out the minimal DOM changes to get there. You describe the destination; React handles the journey.

Setting up a project

The fastest modern start is Vite. It scaffolds a project with a dev server, hot reloading and a build step in one command:

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

Components

A component is just a JavaScript function that returns markup. The name must start with a capital letter — that's how React tells your components apart from plain HTML tags:

Welcome.jsx
function Welcome() {
return <h1>Hello, Albero!</h1>;
}
export default Welcome;

Components compose — you build big UIs by nesting small components, each responsible for one piece:

composition
function Header() {
return <header><h1>Albero Academy</h1></header>;
}
function App() {
return (
<div>
<Header />
<Welcome />
</div>
);
}

JSX rules

That HTML-in-JavaScript is JSX. It looks like HTML but is really JavaScript, so it has a few rules:

  • Return one root element. Wrap siblings in a parent or an empty <>…</> fragment.
  • class becomes className (class is a reserved JS word), and attributes are camelCase (onClick, htmlFor).
  • Embed any JavaScript expression in { } — a variable, a calculation, a function call.
  • Every tag must close, including empty ones: <img />, <br />.
expressions in JSX
function Greeting() {
const name = "Aanya";
const hour = new Date().getHours();
return (
<div className="greeting">
<h2>Hello, {name}!</h2>
<p>{hour < 12 ? "Good morning" : "Good afternoon"}</p>
<p>2 + 2 = {2 + 2}</p>
</div>
);
}

Practice

Scaffold a Vite React app. Build a ProfileCard component that renders your name, role and a short bio, and render three of them inside an App component.

Key takeaways

  1. 01React keeps the DOM in sync with your data — you describe the UI, React updates the screen.
  2. 02Vite is the fastest way to start: npm create vite@latest.
  3. 03A component is a function (capitalised name) that returns JSX; compose big UIs from small components.
  4. 04JSX rules: one root element, className not class, camelCase attributes, {expressions}, self-close every tag.
  5. 05Keep components small and single-purpose so they stay reusable.