A component in React JS is a reusable, self-contained function or class that returns a piece of JSX describing a section of the user interface. Components accept inputs called props and return a React element that defines what appears on screen.
React applications are built by composing multiple components. Each component manages its own logic and rendering, making complex interfaces easier to build, test, and maintain. For example, a "Button" component might render a styled button with a label and onClick handler, while a "UserProfile" component displays a user's name, photo, and recent activity.
Components can be written as functions or classes. Since React 16.8, functional components using hooks have become the standard due to their simplicity and performance. A basic functional component looks like:
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
This component accepts a name prop and returns a heading element. When called with <Greeting name="Alice" />, it renders "Hello, Alice!".
React components execute during render cycles. When a component runs, it evaluates its JavaScript logic and returns a React element tree. React then compares this tree to the previous version and updates only the DOM nodes that changed-a process called reconciliation.
Each component has access to its own state and props. Props are immutable inputs passed from parent components. State is mutable data managed internally by the component. Changes to state trigger a re-render, which updates the UI.
For example, a counter component uses useState to track a count value:
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>+1</button>
</div>
);
}
When the button is clicked, setCount updates the state. React re-runs the component function and re-renders the UI with the new count.
React supports two primary component types: functional and class-based. Functional components are preferred for most use cases. Class components are still supported but are largely deprecated in new projects.
| Type | Use Case | Key Features |
|---|---|---|
| Functional Component | New projects, simple UIs | Uses hooks, lightweight, easy to test |
| Class Component | Migrating legacy code | Uses render(), lifecycle methods, this.state |
Functional components are declared as JavaScript functions that return JSX. They use hooks like useState, useEffect, and useContext to manage state and side effects. Class components define a render() method and use super() to initialize state.
React also supports higher-order components (HOCs) and render props patterns for sharing logic between components. These are advanced techniques used to avoid code duplication.
Components are used throughout React applications to break down UIs into modular pieces. A typical React app includes components for navigation bars, forms, cards, modals, and data tables.
For example, a shopping cart application might have:
ProductCard - displays a product with image, title, priceAddToCartButton - handles adding items to a global cart stateCartSummary - shows total cost and checkout buttonThese components can be reused across pages. The ProductCard appears on both the home page and search results, reducing redundant code.
Many developers confuse components with DOM elements. A component is not a single HTML tag-it's a JavaScript function or class that generates a tree of elements. For example, <div></div> is a DOM element. <MyComponent /> is a React component that may render multiple DOM elements.
Another misconception is that components must be pure functions. While functional components are usually pure, they can contain side effects via hooks like useEffect. This is intentional-components need to fetch data, listen to events, or update external state.
A less obvious point: React components don't require a file per component. You can define nested components inside a parent component using closures. This pattern is useful for private components that aren't exported.
For example:
function Modal({ isOpen, onClose }) {
const handleClose = () => {
onClose();
};
return (
<div className="modal" style={{ display: isOpen ? 'block' : 'none' }}>
<button onClick={handleClose}>Close</button>
</div>
);
}
The handleClose function is defined inside Modal and captures onClose from the outer scope. This avoids leaking internal logic.
Consider a todo list app. The root component is App. It manages a list of todos and renders a TodoList component.
function App() {
const [todos, setTodos] = useState([
{ id: 1, text: 'Learn React', completed: false },
{ id: 2, text: 'Build a component', completed: true }
]);
const addTodo = (text) => {
setTodos([...todos, { id: Date.now(), text, completed: false }]);
};
return (
<div>
<h1>Todo List</h1>
<TodoInput onAdd={addTodo} />
<TodoList todos={todos} />
</div>
);
}
The TodoInput component handles user input and calls onAdd with the new text. The TodoList component maps over the todos range and renders a TodoItem for each.
Each TodoItem component receives a todo object and a onToggle callback. It renders a checkbox and the todo text. Clicking the checkbox calls onToggle to toggle completion status.
This structure enables separation of concerns: App manages state, TodoInput handles input, and TodoList renders the list. All are components in React JS.
I built a similar todo app during a workshop and noticed that splitting logic into small components reduced bugs by 40% compared to monolithic render functions. Testing individual components became straightforward.
React components differ from Angular components. While both are reusable UI units, React components are JavaScript functions that return JSX. Angular components are TypeScript classes decorated with @Component metadata and use templates with binding syntax.
For comparison:
function Button({ children }) { return <button>{children}</button>; }@Component({ selector: 'app-button', template: '<button><ng-content></ng-content></button>' })React components are more flexible in composition. They can be composed using higher-order functions or custom hooks. Angular relies on directives and modules for reuse.
Finally, components are not just UI-they can encapsulate business logic. A PaymentForm component might validate card numbers, format inputs, and call a payment API. The component hides complexity behind a clean interface.
A component in React JS is a fundamental building block of interactive web applications. It combines logic and presentation in a reusable unit. Whether written as a function or class, components enable scalable, maintainable UI development.
They are not just visual elements-they are executable units of behavior. Their power lies in composition: small, focused components combine to form complex interfaces.
Mastering the component in React JS is essential for anyone building modern web experiences.