October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

What Is a React Component? A Beginner’s Guide

A React component is a reusable UI unit, usually a JavaScript function that returns JSX. Learn how components compose and how props, state, and naming work.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A React component is a reusable piece of user interface. It can describe something small, such as a button or avatar, or a larger part of an app, such as a sidebar or page. In modern React, components are usually JavaScript functions that return JSX, and they can be combined to build an interface.

What a React component looks like

Here is a small function component and an app that uses it:

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

export default function App() {
  return <Greeting name="Sam" />;
}

Greeting is a function component. It receives a value named name, then returns JSX describing a heading. The App component renders it with name set to "Sam". React’s beginner guide describes components as reusable UI elements: React: Your First Component.

How components fit together

A component is a named unit of UI that React can render. Components can be nested inside other components, so a page might be assembled from smaller parts such as a navigation bar, profile card, and button. A component can also represent a larger portion of an application; React does not require every project to use one particular page structure or framework.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Think of a component as a recipe for part of the interface: its inputs and its own changing data determine what it describes. That is an analogy, not React terminology. The practical idea is to give each component a clear job, then compose components through JSX.

JSX is the description, not the component itself

JSX is a JavaScript syntax extension for describing markup. It resembles HTML, but it is not HTML. A function component returns JSX or another valid React node, and React uses that result to render the interface. In the example, <h1> is JSX for a built-in HTML element, while <Greeting /> asks React to render a component.

JavaScript expressions can appear inside JSX braces. For example, {"{name}"} inserts the value of the name variable into the heading. When a returned JSX expression spans multiple lines, wrap it in parentheses:

function Greeting({ name }) {
  return (
    <h1>Hello, {name}!</h1>
  );
}

More about JSX syntax is in React: Writing Markup with JSX.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Props: values a component receives

Props are inputs supplied by a parent component. In <Greeting name="Sam" />, the parent passes the string "Sam" through the name prop. The child reads it, often by destructuring the props object in its function parameters, as Greeting does above.

Props are read-only from the child’s perspective: a component should not mutate the values it receives. They are not limited to strings; a parent can pass objects, arrays, functions, and JSX as props. Use props when a component needs information provided from outside. See React: Passing Props to a Component.

State: information that changes over time

State is component-specific memory for information that can change, often in response to an interaction. A function component can use the useState Hook to read a state value and request an update. For example, a counter’s current number belongs in state if clicking a button changes it; a person’s name passed in by a parent belongs in props if the component simply displays it.

Keep the distinction in mind: props are supplied from outside and read by the component; state is the component’s changing memory. React’s guide to Adding Interactivity explains state and interaction.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Component naming and placement rules

  • Capitalize component names. Use names such as Greeting and render them as <Greeting />. Lowercase JSX tags such as <h1> refer to built-in HTML elements.
  • Render a component through JSX. Use <Greeting name="Sam" /> rather than calling Greeting() as an ordinary function. JSX lets React manage component rendering.
  • Define components at the top level. Do not define a component inside another component’s function. If the inner component needs data from its parent, pass that data through props.
  • Follow the Rules of React. In particular, call Hooks such as useState at the top level of a function component, not conditionally or inside a nested function. See React: Rules of React.

Function components and older class components

React still supports class components, so you may encounter them in existing code. However, React recommends defining components as functions for new code. This guide uses function components as the current beginner path; the React Component reference documents class components and their supported status.

When you use React components

Components can describe all of an application’s UI or selected interactive parts of a page. React is a JavaScript library for rendering user interfaces, and frameworks built on React may also generate HTML from components. The right component boundaries depend on the interface: split out parts that are useful to reuse, understand, or update independently, rather than assuming every small fragment needs its own component.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.