🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

Components

AI & DATA SCIENCE // components

Components are independent, reusable pieces of UI in React, typically written as JavaScript functions that return JSX describing what should render.

Syntax

function MyComponent(props) {
  return <div>{props.children}</div>;
}

Deep Dive Course

A React component is fundamentally just a JavaScript function that accepts a single props object as its argument and returns JSX describing what should appear on screen — component names must start with a capital letter, which is exactly how React and JSX distinguish a custom component like <MyComponent /> from a plain HTML tag like <div>. Components can be composed together, nesting smaller components inside larger ones, which is the core mechanism React relies on for building complex UIs out of small, independently understandable, reusable pieces.

1Understanding Components

A React component is fundamentally just a JavaScript function that accepts a single props object as its argument and returns JSX describing what should appear on screen — component names must start with a capital letter, which is exactly how React and JSX distinguish a custom component like <MyComponent /> from a plain HTML tag like <div>. Components can be composed together, nesting smaller components inside larger ones, which is the core mechanism React relies on for building complex UIs out of small, independently understandable, reusable pieces.

💡

Component names must start with a capital letter — lowercase names like <mycomponent /> are treated as plain HTML tags by JSX, not as your custom component, which produces confusing errors or silently renders nothing.

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

function App() {
  return <Greeting name="Ana" />;
}
localhost:3000

2Practical Example

Here is a real-world application of Components showing how it is used in production React code.

editor.html
function Card({ title, children }) {
  return (
    <div className="card">
      <h2>{title}</h2>
      {children}
    </div>
  );
}

function App() {
  return <Card title="Profile"><p>Bio text here.</p></Card>;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Components:

1. Start every component's name with a capital letter, since JSX uses that capitalization to distinguish custom components from built-in HTML tags

2. Keep each component focused on a single responsibility, composing complex UIs out of several small, focused components rather than one large one

3. Pass data into a component exclusively through its props, rather than reaching into external state directly from inside the component

⚠️

Tip: Component names must start with a capital letter — lowercase names like <mycomponent /> are treated as plain HTML tags by JSX, not as your custom component, which produces confusing errors or silently renders nothing.

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

function App() {
  return <Greeting name="Ana" />;
}
localhost:3000

4Components Must Be Pure

Given the same props, a component's render logic should always produce the same JSX, with no side effects like mutating an outside variable or firing a network request during render. React may call a component function more than once per commit — StrictMode double-invokes it in development specifically to catch this — so anything that shouldn't run twice belongs in an event handler or useEffect, never directly in the render body.

editor.html
// Impure: mutates something outside during render
let renderCount = 0;
function Profile() {
  renderCount++;
  return <p>{renderCount}</p>;
}
localhost:3000

5Composition Over Inheritance: the children Prop

React favors composition over inheritance for sharing behavior. A component that accepts the special children prop can wrap whatever JSX its caller passes between its opening and closing tags, letting a generic wrapper like Card stay completely agnostic about the content it contains.

editor.html
function Card({ title, children }) {
  return <div className="card"><h2>{title}</h2>{children}</div>;
}

<Card title="Bio"><p>Any JSX at all.</p></Card>
localhost:3000

Examples

Example 01Basic Usage
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

function App() {
  return <Greeting name="Ana" />;
}
Example 02Advanced Example
function Card({ title, children }) {
  return (
    <div className="card">
      <h2>{title}</h2>
      {children}
    </div>
  );
}

function App() {
  return <Card title="Profile"><p>Bio text here.</p></Card>;
}

Best Practices

  • Start every component's name with a capital letter, since JSX uses that capitalization to distinguish custom components from built-in HTML tags
  • Keep each component focused on a single responsibility, composing complex UIs out of several small, focused components rather than one large one
  • Pass data into a component exclusively through its props, rather than reaching into external state directly from inside the component
  • Keep a component's render logic pure — no mutating outside variables, no network requests, no logging side effects during render itself
  • Reach for the children prop to build generic wrapper components (layouts, cards, modals) instead of duplicating a component for every kind of content it might hold

Interview Question

Why does JSX require component names to start with a capital letter?

Hint: Think about how JSX needs to distinguish between a built-in HTML element and a reference to your own JavaScript function/variable.

When JSX compiles a tag like <div> or <Greeting />, it needs a rule to decide whether that tag should become a plain string, referring to a built-in HTML element the browser already understands, or a reference to an actual JavaScript variable representing your component function. JSX's compiler uses capitalization as exactly that rule: a lowercase tag name compiles to a plain HTML element string, while a capitalized tag name compiles to a reference to a JavaScript identifier with that same name, which React then calls as a function, or a class, to get the rendered output. This is purely a JSX/React convention, not a JavaScript language rule, but it's why naming a component starting with a lowercase letter causes JSX to treat it as an unrecognized HTML tag rather than correctly resolving it as your custom component.

Exercises

MediumPractice using Components in a real scenario.
View Solution
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

function App() {
  return <Greeting name="Ana" />;
}

Frequently Asked Questions

Why does JSX require component names to start with a capital letter?

When JSX compiles a tag like

or , it needs a rule to decide whether that tag should become a plain string, referring to a built-in HTML element the browser already understands, or a reference to an actual JavaScript variable representing your component function. JSX's compiler uses capitalization as exactly that rule: a lowercase tag name compiles to a plain HTML element string, while a capitalized tag name compiles to a reference to a JavaScript identifier with that same name, which React then calls as a function, or a class, to get the rendered output. This is purely a JSX/React convention, not a JavaScript language rule, but it's why naming a component starting with a lowercase letter causes JSX to treat it as an unrecognized HTML tag rather than correctly resolving it as your custom component.

Why does React call a component function twice in development under StrictMode?

StrictMode intentionally double-invokes a component's render logic (and certain effects) in development to help surface impure code — if a component's render function has a hidden side effect, like mutating a variable outside itself or reading and writing shared state, calling it twice makes that impurity visible as an obviously wrong result, rather than it silently working by accident most of the time and breaking unpredictably under concurrent rendering later. It does not double-render in production; the extra call is a development-only diagnostic.

Related Functions

jsxpropsfunctional-components