A React component is a JavaScript function that returns JSX: a reusable piece of UI such as a button, a card or a whole page. You use it like an HTML tag, <Profile />, and React calls the function to find out what to put on the screen. Apps are built by putting components inside other components.
App renders Profile twice, and each Profile renders an Avatar. Change the initials inside Avatar and both profiles update, because they share one definition. Every person in this list is the same right now; the next page, props, shows how to pass each copy its own data.
Writing a component
A function component has two requirements:
- Its name starts with a capital letter:
Profile, notprofile. - It returns something React can render: JSX, a string, a number,
null(render nothing) or an array of those.
function Welcome() {
return <h1>Welcome back</h1>;
}
// Arrow functions work the same way
const Welcome = () => <h1>Welcome back</h1>;
When the JSX spans several lines, wrap it in parentheses after return. Without them, a return followed by a line break returns undefined, and everything after it is ignored.
Why the name must be capitalized
JSX looks at the first letter of a tag. Lowercase means a built-in HTML element, so <div> stays <div>. Capitalized means "a variable in scope", so <Profile /> calls your Profile function. Name a component in lowercase and React creates an unknown HTML element with that name instead:
The second line shows only its label, and the console only shows Greeting ran. React put an empty <greeting> tag into the page and never called the lowercase function. A development build also warns that The tag <greeting> is unrecognized in this browser; the preview runs like a production build, so you only see the missing text. Rename the function to Greetings and the tag to <Greetings /> to fix it.
Composing components
Components are meant to be nested. A page is a tree: App at the top, then layout pieces, then smaller parts down to single buttons. Each component only knows about the pieces it renders directly, so you can reason about one at a time.
Each LikeButton has its own state, so liking one post leaves the others alone. To add a share button to every post, you write it once and add <ShareButton /> inside Post.
When to split a component
There is no fixed rule, but a few signs say a piece deserves its own component: the same markup appears in two places, a section of the JSX has its own state (like the like button above), or the function has grown long enough that you scroll to see its return. Splitting along those lines keeps each component small enough to read in one screen. Splitting too early costs little: a component is only a function, so merging two back together is easy.
Exporting and importing components
The editors here hold one file, but a real project puts components in their own files. A file can have one default export and any number of named exports:
// Avatar.jsx
export default function Avatar({ initials }) {
return <div className="avatar">{initials}</div>;
}
// Badge.jsx
export function Badge({ label }) {
return <span className="badge">{label}</span>;
}
export function NewBadge() {
return <Badge label="new" />;
}
// Profile.jsx
import Avatar from './Avatar.jsx';
import { Badge, NewBadge } from './Badge.jsx';
export default function Profile() {
return (
<div>
<Avatar initials="AL" />
<Badge label="admin" />
<NewBadge />
</div>
);
}
A default export can be imported under any name; a named export is imported in braces with its exact name. Teams usually pick one style and stick with it. File names commonly match the component: Avatar.jsx exports Avatar.
Keep components pure
React expects a component to behave like a pure function while it renders: the same props and state give the same JSX, and rendering changes nothing that existed before it ran. React may render a component many times, at times you do not control, so a component that edits outside variables during render produces different output each time.
The impure cups start at 1, 2, 3. Click the button: nothing about the guests changed, yet the impure list now reads 4, 5, 6, because each render added to the shared guest variable. The pure cups get their number from props and stay correct. In development, Strict Mode renders every component twice to expose exactly this kind of bug; the preview runs like a production build, so here the drift only shows when you render again.
Changing things is fine, just not during render. Update state in event handlers, and talk to the outside world (timers, subscriptions, the document title) in effects.
Components vs elements
The component is the function, Profile. An element is what JSX creates when you write <Profile />: a small object saying "render Profile with these props". React creates elements, calls the components they name, and turns the result into DOM. You never call a component yourself as Profile(); you write <Profile /> and let React call it, so it can track its state and its place in the tree.
That place matters. This example defines a component inside another component, which is a common mistake:
Type something, then click the button: your text disappears. Every render of App creates a brand new NameField function, so React sees a different component type in that spot and throws the old one away with its state. Move NameField above App, at the top level of the file, and the text survives every click. The page on rendering explains how React decides what to keep.
Function components and class components
Before hooks arrived in React 16.8, components that needed state were written as classes:
import { Component } from 'react';
class Welcome extends Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Class components still work in React 19, and you will meet them in older codebases and in error boundaries, which still require a class. New code uses function components with hooks such as useState and useEffect, which do the same jobs with less code. The class components page maps each class feature to its hook.
Frequently Asked Questions
What is a component in React?
A component is a reusable piece of UI written as a JavaScript function that returns JSX. You use it like an HTML tag, <Profile />, and React calls the function to find out what to draw.
Why must React component names start with a capital letter?
JSX uses the first letter to decide what a tag is. A lowercase tag such as <profile /> is treated as an HTML element named profile, and your function is never called. A capitalized tag such as <Profile /> refers to your component.
What is the difference between function and class components?
Both describe UI. Function components are plain functions and use hooks for state and effects; class components extend React.Component and use methods such as render and componentDidMount. New code uses function components.
What is a pure component in React?
A pure component returns the same JSX for the same props and state and changes nothing outside itself while it renders. React relies on this to render components as often as it needs to without surprises.
Can I define a component inside another component?
Avoid it. A component defined inside another one is a new function on every render, so React treats it as a different component each time and resets its state. Define components at the top level of the file.