Your first component
Overview
Components are the building blocks of every React app. In this lesson you’ll write your own, split them into files, nest them, and learn the handful of rules that make them work.
Defining a component
function Greeting() {
return <h1>Welcome to the React course!</h1>;
}That’s a complete component. Three things make it one:
- It’s a function. (Older code uses classes — you’ll still see
class X extends React.Componentin legacy projects, but function components are the modern standard.) - Its name starts with a capital letter. React uses this to tell your components apart from HTML tags.
- It returns JSX — markup describing what to show. It can also return
nullto show nothing.
Using a component
You use a component like an HTML tag:
export default function App() {
return (
<main>
<Greeting />
<p>Let’s get started.</p>
</main>
);
}React sees <main> and <p> (lowercase) and creates DOM elements. It sees <Greeting /> (capitalised) and calls your Greeting function, then renders whatever it returns. If you wrote <greeting />, React would try to create an unknown HTML element called greeting — nothing would appear, and you’d get a console warning.
Self-closing tags must end with />: <Greeting />, <img />, <br />.
Multi-line JSX needs parentheses
function CourseCard() {
return (
<article>
<h2>React</h2>
<p>49 lessons</p>
</article>
);
}Without the parentheses, JavaScript’s automatic semicolon insertion would put a semicolon after return, and the function would return undefined.
Components are a tree
Components can render other components, which render others, forming a tree:
App is the root. Thinking in trees helps with almost everything that comes later: data flows down the tree as props, state lives at a particular node, errors bubble up to boundaries, and so on.
One component per file
As your app grows, move components into their own files:
// src/components/CourseCard.jsx
export default function CourseCard() {
return (
<article className="card">
<h2>React</h2>
<p>49 lessons</p>
</article>
);
}// src/App.jsx
import CourseCard from './components/CourseCard.jsx';
export default function App() {
return (
<main>
<CourseCard />
<CourseCard />
</main>
);
}Default vs named exports
// Default export — one per file, import with any name
export default function CourseCard() { … }
import CourseCard from './CourseCard.jsx';
// Named exports — many per file, import with the exact name in braces
export function CourseCard() { … }
export function CourseBadge() { … }
import { CourseCard, CourseBadge } from './CourseCard.jsx';Both are fine; many teams prefer named exports because editors and refactoring tools handle them more reliably. Pick one convention and stick to it.
The most common beginner mistake
Never define a component inside another component:
// ✗ Don't do this
function CourseList() {
function CourseCard() { // a NEW function on every render
return <article>…</article>;
}
return <CourseCard />;
}Every time CourseList renders, it creates a brand-new CourseCard function. React sees a “different” component type each time, so it throws away the old DOM and state and builds new ones — slow, and any state inside is lost. Always define components at the top level of a module, and pass data in with props (next chapter).
Components must be pure
React expects components to behave like pure functions while rendering:
- Same inputs → same output. Given the same props and state, a component should always return the same JSX.
- No side effects during render. Don’t change variables outside the component, modify the DOM directly, start timers or send requests in the function body.
let guestCount = 0;
// ✗ Impure: changes a variable outside itself
function Guest() {
guestCount = guestCount + 1;
return <p>Guest #{guestCount}</p>;
}Render <Guest /> three times and you’d expect 1, 2, 3 — but StrictMode renders twice in development, so you’d see 2, 4, 6. That’s exactly the kind of bug StrictMode is designed to reveal. Side effects belong in event handlers (when the user does something) or effects (to sync with outside systems), which you’ll meet later.
Creating and changing local variables inside the component is completely fine:
function CourseTags() {
const tags = []; // local — created fresh each render
tags.push('hooks', 'state');
return <p>{tags.join(', ')}</p>;
}Fragments
A component must return a single root element. To return siblings without adding a wrapper <div>, use a Fragment:
function CourseMeta() {
return (
<>
<dt>Lessons</dt>
<dd>49</dd>
</>
);
}<>…</> groups elements without adding anything to the DOM — handy inside lists, tables and definition lists, where an extra <div> would produce invalid HTML.
Naming: name components after what they are, not how they look:
CourseCard,PriceTag,EnrollButton— notBlueBoxorLeftThing.
Build a profile card
Create src/components/InstructorCard.jsx that exports a default InstructorCard component rendering an <article> with an image (src of https://i.pravatar.cc/120?img=5 and a meaningful alt), an <h2> with the name “Priya Sharma”, and a <p> with “React instructor · 8 years”. Then render three InstructorCards inside a <section> in App.
// src/components/InstructorCard.jsx
export default function InstructorCard() {
// your code here
}
// src/App.jsx
import InstructorCard from './components/InstructorCard.jsx';
export default function App() {
// your code here
}// src/components/InstructorCard.jsx
export default function InstructorCard() {
return (
<article className="instructor">
<img src="https://i.pravatar.cc/120?img=5" alt="Priya Sharma" width="120" height="120" />
<h2>Priya Sharma</h2>
<p>React instructor · 8 years</p>
</article>
);
}
// src/App.jsx
import InstructorCard from './components/InstructorCard.jsx';
export default function App() {
return (
<section>
<h1>Our instructors</h1>
<InstructorCard />
<InstructorCard />
<InstructorCard />
</section>
);
}Summary
- A component is a JavaScript function that returns JSX; its name must start with a capital letter.
- Lowercase tags (
<div>) are HTML elements; capitalised tags (<Card />) are your components. - Put one main component per file and use
export defaultor named exports to share it. - Never define a component inside another component — define every component at the top level.
- Components must be pure: same inputs, same output, no side effects while rendering.