Course topics

By WebNest Studio Engineering Team

React Tutorial

React Components

A component is a self-contained, reusable piece of UI described as a JavaScript function that returns JSX. Components are the fundamental building block of every React application — a whole app is really just a tree of components nested inside one another, starting from a single root component rendered into the page.

Modern React code is written almost entirely with function components rather than the older class-based components. A function component is simply a JavaScript function whose name starts with a capital letter and which returns JSX describing what should appear on screen. Capitalization matters: React treats lowercase tags like <div> as built-in HTML elements and capitalized tags like <UserCard /> as references to your own components.

Why it matters

A React application is a tree of components, each responsible for one piece of the screen. Splitting an interface into small, named components is what keeps a large application understandable and lets a piece such as a button or a card be reused everywhere.

Writing a Function Component

A function component takes a single object of props as its argument (often destructured directly in the parameter list) and returns JSX. It can contain plain JavaScript logic above the return statement — computing values, calling hooks, formatting data — before describing the resulting markup.

Composition Over Inheritance

React favors composition: instead of building deep class hierarchies, you build UI by nesting smaller components inside larger ones and passing data or even other components through props. A component can render other components, which in turn render more components, forming a tree that mirrors the structure of the rendered page.

Splitting UI into Small Components

Breaking a large UI into small, focused components keeps each piece easy to understand, test, and reuse. A good rule of thumb is that if a chunk of JSX represents one clear concept (a card, a button, a form field) or is duplicated in more than one place, it is a good candidate to extract into its own component.

  • Each component should ideally do one thing — rendering a user profile, a single list item, or a navigation bar — rather than mixing many unrelated responsibilities.
  • Small components are easier to give clear prop interfaces to, which makes their behavior predictable from the outside.
  • Component trees make it easy to reuse the same building block in many places, such as a Button or Card component used across an entire application.

Example

A parent component composed from two smaller child components

JavaScript
function Avatar({ src, alt }) {
  return <img className="avatar" src={src} alt={alt} width={48} height={48} />;
}

function UserName({ name, title }) {
  return (
    <div>
      <strong>{name}</strong>
      <p>{title}</p>
    </div>
  );
}

function UserCard({ user }) {
  return (
    <div className="user-card">
      <Avatar src={user.avatarUrl} alt={user.name} />
      <UserName name={user.name} title={user.title} />
    </div>
  );
}

export default function App() {
  const user = { name: 'Priya Rao', title: 'Frontend Engineer', avatarUrl: '/priya.jpg' };
  return <UserCard user={user} />;
}
Output
Renders a "user-card" container showing a 48x48 avatar image next to the bold name "Priya Rao" and the subtitle "Frontend Engineer", built by composing three separate components (Avatar, UserName, UserCard).

Common Mistakes

  • Starting a component function name with a lowercase letter, which makes React treat it as an unrecognized HTML tag instead of a component.
  • Cramming an entire page into one giant component instead of splitting it into smaller, focused pieces.
  • Defining a component function inside the body of another component (rather than at module scope), which causes it to be recreated on every render and lose its state.
  • Returning nothing (undefined) from a component instead of null or valid JSX, which throws a rendering error.

Exercise

Do this in src/App.jsx of a Vite + React project. Split the page below into three components: Header, which renders the title; Footer, which renders the copyright line; and App, which composes them around the main paragraph.

Expected result: the page looks exactly the same, but App now uses <Header /> and <Footer />.

JavaScript · starter code
export default function App() {
  return (
    <div>
      <header>
        <h1>WebNest Learn</h1>
      </header>
      <main>
        <p>Pick a course to begin.</p>
      </main>
      <footer>
        <p>Copyright WebNest Studio</p>
      </footer>
    </div>
  );
}
Show hints
  • Each component is a function whose name starts with a capital letter and which returns JSX.
  • A component is used like a tag: <Header />.
Show solution
JavaScript · solution
function Header() {
  return (
    <header>
      <h1>WebNest Learn</h1>
    </header>
  );
}

function Footer() {
  return (
    <footer>
      <p>Copyright WebNest Studio</p>
    </footer>
  );
}

export default function App() {
  return (
    <div>
      <Header />
      <main>
        <p>Pick a course to begin.</p>
      </main>
      <Footer />
    </div>
  );
}

Quick quiz

1. Why must the name of a component start with a capital letter?

  • A. It is only a style convention
  • B. JSX treats lower-case tags as HTML elements and capitalised tags as components
  • C. Lower-case names are reserved
  • D. It makes the component faster
Show answer

B. <header> is the HTML element; <Header> is your component.

2. What is a function component?

  • A. A class that extends React.Component
  • B. A CSS file
  • C. A JavaScript function that returns JSX
  • D. An HTML template
Show answer

C. Function components with hooks are the standard way to write React today.

3. How does React encourage code reuse between components?

  • A. Composition: building components out of other components
  • B. Inheritance between component classes
  • C. Copying and pasting
  • D. Global variables
Show answer

A. A component receives other components or elements as props and children.

Interview questions

What is the difference between a function component and a class component?

Show answer

A function component is a plain function that receives props and returns JSX, using hooks for state and side effects. A class component extends React.Component, keeps state in this.state and uses lifecycle methods such as componentDidMount. Function components are shorter, avoid this, and let logic be shared through custom hooks; they are the recommended form. Classes remain in older code and are still required for error boundaries.

How do you decide when to split a component?

Show answer

Split when a piece of the interface is reused elsewhere, when a component does more than one job, when it has grown too long to read comfortably, or when part of it has its own state that the rest does not need. A good component has a single responsibility and a name that describes it. Splitting too early, before any of these apply, only adds files and props to pass.

Key Points to Remember

  • A React component is a capitalized JavaScript function that returns JSX describing part of the UI.
  • Applications are built as a tree of components composed inside one another, not through class inheritance.
  • Splitting UI into small, focused components improves reuse, readability, and testability.
  • Component functions should be defined at the top level of a module, not nested inside another component.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of plain HTML, CSS and JavaScript. JSX needs a React project on your computer. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering