Chapter 3 of 20 · React for Beginners — Complete Free Tutorial

Your First Component

Now that the React for Beginners development server is running, you can write real React. A component is the atom of every React application: a function that returns a description of UI. This chapter builds one from scratch, composes it inside another, and lays down the naming rules that keep a codebase readable.

What a component is

Think of a component as a recipe that produces markup for the current inputs. It has a name, optionally receives inputs (props — chapter 5), and returns JSX. Because it is a function, you can call it, test it and reuse it anywhere the same piece of screen is needed.

  • One function, one piece of UI — a navbar, a ticket row, a pricing card.
  • Capitalised names — React treats <Greeting /> and <greeting /> differently; the capital tells JSX it is a component.
  • No side effects — rendering should only describe UI; fetching or subscribing belongs in effects (chapter 12).

Your first component

Create src/components/Greeting.jsx and export the function. The export default line is what lets other files import it:

export default function Greeting() {
  return (
    <div className="card">
      <h1>Hello, React!</h1>
      <p>My first component renders this paragraph.</p>
    </div>
  );
}

Expected output: a card containing the heading Hello, React! and the paragraph below it, styled by any CSS you have applied to .card. Note the single wrapper <div>: until fragments arrive, a component returns one root element.

Using a component inside another

Components shine when nested. Import each one into the file where it is used — composition is just functions calling functions:

import Greeting from './components/Greeting.jsx';

function Toolbar() {
  return (
    <header>
      <nav>Scriptly</nav>
      <Greeting />
    </header>
  );
}

export default Toolbar;

Expected output: the page shows Scriptly navigation followed by the greeting card — two independent files working as one screen. Each component can be edited, styled and reasoned about without opening the other.

Imports, exports and files

React projects use the ES modules you may know from the modules chapter: every file is a module, and explicit imports connect them. Vite resolves relative paths (./components/Greeting.jsx) at dev time and rewrites them for production.

ConventionExampleWhy
Component file nameGreeting.jsxMatches the component it exports
Component nameGreetingPascalCase marks it as a component in JSX
Exportexport default function Greeting()One clear entry point per file
Import pathimport Greeting from './Greeting.jsx'Explicit, relative, with extension

Keep one component per file when a component grows past a screenful of code — that is the natural point to split it.

Rendering your component

For a quick experiment, render straight from App.jsx and watch the dev server update:

import Greeting from './components/Greeting.jsx';

export default function App() {
  return (
    <main>
      <Greeting />
      <p>Everything else the page needs.</p>
    </main>
  );
}

Expected output: the greeting card followed by the paragraph. If nothing appears, the browser console usually reveals the classic causes: a lowercase tag, a missing import, or a typo in the file path.

How React sees your tree

When App renders, React builds a tree from the tags you nested: main contains Greeting (whose output is a card) and a p. That tree is what gets diffed on every update, and it is also what tools like the React Developer Extensions inspector display. Thinking in trees — rather than in files — will make conditional rendering and keys in later chapters feel obvious.

Common mistakes

  • Lowercase component tags — <greeting /> tells React to look for an HTML element named greeting; use <Greeting />.
  • Forgetting the import — a component defined in another file renders nothing until you import it (and Vite will flag the name as undefined).
  • Returning sibling elements without a wrapper — two root elements at once throw a syntax error; wrap them in one <div> or later a fragment.
  • Calling the component as a function — write <Greeting />, not {Greeting()}; the JSX form lets React manage it correctly.
  • Mismatched file names — import Greeting from './greeting' fails on case-sensitive systems; keep names identical.

Practice checklist

  1. Create Greeting.jsx exactly as shown and render it from App.jsx.
  2. Add a second component called Footer and compose both inside a Page component.
  3. Break the capitalisation on purpose (<greeting />), observe the result, then fix it.
  4. Remove an import, read the browser console error, and restore it.
  5. Nest a component three levels deep and draw the resulting tree on paper.

Your components render, but their markup deserves a closer look — JSX Explained is next.

Quiz: Your First Component

5 questions · 70% to pass

Frequently Asked Questions

React is a JavaScript library for building user interfaces from reusable components. It manages how your UI updates when data changes, so you describe what the screen should look like and React performs the efficient DOM updates. It is one of the most in-demand frontend skills and powers products from Netflix to Instagram, which makes it an excellent next step after core JavaScript.

You should be comfortable with functions, arrays and objects (map, filter, spread), destructuring, modules and basic async/await. React itself is written in plain JavaScript — JSX is a syntax layer on top. If any of those feel shaky, revisit the JavaScript fundamentals first and this series will go much faster.

React is a library focused only on the UI layer — you choose your own router, data layer and tooling. Angular is a full framework that ships routing, forms, HTTP client and testing conventions as one opinionated package. React's flexibility and huge ecosystem make it the common choice for teams that want to compose their own stack.

A component is a reusable function that returns UI. It receives input through props, keeps private data in state, and can render other components. Pages are composed by nesting small components (navbar, card list, footer) instead of one giant file, which keeps code testable and easy to change.

JSX is a syntax extension that looks like HTML but compiles to React's create() calls — it is a description of UI, not markup the browser parses. Small differences matter: class becomes className, style takes an object, and you embed JavaScript expressions inside curly braces. Once compiled, React turns that description into real DOM nodes.

Props are inputs passed from a parent — they are read-only inside the child, like function arguments. State is private, mutable data owned by a component; updating it triggers a re-render. The rule of thumb: values that come from outside the component are props; values that change over the component's lifetime are state.

Rendering means calling your component function to produce a description of the UI for the current state and props. It does not always mean DOM work — React compares the new description with the previous one (reconciliation) and applies only the differences to the real DOM. This is why updating state 100 times does not mean 100 full page rebuilds.

An effect runs after the component has committed to the screen — after render, not during it. You list dependencies in an array, and React re-runs the effect only when one of them changes. If the effect returns a cleanup function, React runs it before the next re-run and on unmount, which is where you cancel timers, abort fetches and remove listeners.

Use Vite. Create React App is effectively in maintenance mode, while Vite starts instantly (native ES modules) and builds fast, and it is the default choice for new React projects. This series uses Vite for every example — you only need Node.js installed to follow along.

React apps compile to static files (HTML, CSS and JavaScript), so any static host works: Netlify, Vercel, GitHub Pages or Cloudflare Pages. Run the production build, upload the dist folder, and configure a single-page-application fallback so unknown paths load index.html — routing chapters explain why that fallback is required.