EduFlow
React Fundamentals
0%

Section 1 · Getting Started

Your First Component

18 min3 min readCode playground

Lesson at a glance

Write React components as plain functions, nest them, split them into files with imports and exports, and avoid the naming and nesting mistakes that break them.

  1. 1A component is a function
  2. 2Components inside components
  3. 3One component per file
  4. 4Components should be predictable
  5. 5Return a single root

No video here — just the real thing: a written lesson, runnable examples, a quiz and practice.

Right now App.jsx holds one heading. A real Watchlist screen has a header, a form, a filter bar and a list of movie cards, and some pieces appear many times. If you write it all in one function you get a 300-line component that nobody wants to touch. Components let you name each piece, write it once and reuse it.

A component is a function

Here is a complete component:

src/App.jsx
function Header() {
  return (
    <header>
      <h1>My Watchlist</h1>
      <p>Movies I want to see</p>
    </header>
  );
}

export default function App() {
  return (
    <main>
      <Header />
    </main>
  );
}

Three rules make this work:

  1. It’s a function that returns JSX. JSX is the HTML-like syntax inside return. You’ll learn its rules in the next lesson.
  2. Its name starts with a capital letter. <Header /> tells React “call the Header function”. A lowercase <header> means the HTML element.
  3. You use it as a tag, not a function call. You write <Header /> and React decides when to call Header(). That’s what lets React track it, re-render it and give it state later.

The parentheses around the multi-line JSX aren’t required by React. They stop JavaScript’s automatic semicolon insertion from ending the return early, which would silently return undefined.

Components inside components

Components nest like HTML. Add a movie card and use it twice:

src/App.jsx
function MovieCard() {
  return (
    <article>
      <h2>Arrival</h2>
      <p>2016</p>
    </article>
  );
}

export default function App() {
  return (
    <main>
      <Header />
      <MovieCard />
      <MovieCard />
    </main>
  );
}

You now have two identical cards. That’s obviously not useful yet; the cards need to show different movies. Passing data into a component is what props are for, and they’re coming in Section 2. For now, notice the shape: App is the parent, and Header and the two MovieCards are its children. Every React app is a tree like this.

One component per file

When a component is reused or grows past a screenful, give it its own file. The usual layout is a src/components folder:

src/components/MovieCard.jsx
export default function MovieCard() {
  return (
    <article>
      <h2>Arrival</h2>
      <p>2016</p>
    </article>
  );
}
src/App.jsx
import Header from './components/Header.jsx';
import MovieCard from './components/MovieCard.jsx';

export default function App() {
  return (
    <main>
      <Header />
      <MovieCard />
    </main>
  );
}

JavaScript modules give you two kinds of export:

  • Default export: export default function MovieCard(). One per file, imported without braces: import MovieCard from '…'.
  • Named export: export function MovieCard(). Any number per file, imported with braces: import { MovieCard } from '…'.

Both are fine. Many teams prefer named exports because the name is fixed and editors rename it everywhere for you. Pick one convention per project and stick to it. This course uses default exports for components, matching the file Vite generated.

Components should be predictable

React may call your component function many times: on every update, twice in StrictMode, and sometimes when it’s preparing a screen in the background. So a component should behave like a formula. Given the same inputs, it returns the same JSX and doesn’t change anything outside itself while rendering. Don’t modify global variables, fetch data or touch the DOM directly in the function body. You’ll learn where that kind of work belongs (event handlers and effects) in Section 3.

Here’s the difference in practice. A component that does visits += 1 on a module-level variable will report different numbers in development and production, because StrictMode calls it twice. A component that only reads its inputs and returns JSX gives the same answer no matter how often React calls it.

Return a single root

A component returns one JSX element. To return siblings without adding an extra <div>, wrap them in a fragment:

JSX
export default function App() {
  return (
    <>
      <Header />
      <MovieCard />
    </>
  );
}

A fragment groups elements in your code but adds nothing to the DOM. Use a real element like <main> or <section> when it has meaning for the page’s structure, and a fragment when it doesn’t.

You can now split a screen into named pieces. Next you’ll learn the rules of JSX itself: why it’s className and not class, how curly braces work, and what JSX turns into when Vite compiles it.

Key takeaways

  • A component is a JavaScript function whose name starts with a capital letter and that returns JSX.
  • You use a component like an HTML tag, <MovieCard />, and React calls the function for you.
  • Give each reusable component its own file and export it; import it wherever it’s used.
  • Define components at the top level of a module, never inside another component.

Further reading

Check your understanding

4 questions

Question 1 of 4

You write this component and use it as <movieCard />. Nothing appears, and the console warns about an unrecognized tag. Why?

JSX
function movieCard() {
  return <article>Arrival</article>;
}
Question 2 of 4

Which line correctly imports a component that src/components/MovieCard.jsx exports with export default function MovieCard()?

Question 3 of 4

A teammate defines Badge inside MovieCard. What problem does this cause?

JSX
export default function MovieCard() {
  function Badge() {
    return <span>New</span>;
  }
  return <article><Badge /></article>;
}
Question 4 of 4

Your App returns <Header /><MovieCard /> side by side and Vite shows “Adjacent JSX elements must be wrapped in an enclosing tag”. What’s the smallest correct fix?

Practice

Code playground

Header and movie cards

Build the first slice of the Watchlist screen.

  1. Make Header return a <header> containing an <h1> with the text My Watchlist and a <p> tagline of your choice.
  2. Make MovieCard return an <article> with an <h2> reading Arrival and a <p> reading 2016.
  3. In App, render Header once and MovieCard twice inside the <main>.
function Header() {
  // Return a <header> with an <h1> and a <p>
  return null;
}

function MovieCard() {
  // Return an <article> with an <h2> title and a <p> year
  return null;
}

export default function App() {
  return (
    <main>
      {/* Use Header once and MovieCard twice */}
    </main>
  );
}
Loading the editor…

Ctrl/⌘ + Enter to run · Runs in a sandbox in your browser. Nothing leaves your device.

Preview

Console

(no output)

Checks

  • The
    contains an

    reading “My Watchlist”

  • The
    has a

    tagline

  • contains exactly two
    cards
  • Each card has an

    with “Arrival” and a

    with “2016”