React Basics · Components
Components are the core building blocks of a React application. They define what appears on the screen and how it behaves. Each component is an independent, reusable piece of the UI.
In React, components can be defined as functions or classes. Both return JSX to describe the UI, but they differ in syntax and how features like state and lifecycle are handled.
React.Component.useState and useEffect that bring state and lifecycle to functional components.A React component must return JSX, which looks like HTML but is actually JavaScript syntax. React then converts this JSX into DOM elements.
You can define a component in two ways:
A Functional Component is a JavaScript function that returns JSX. It receives data through props and renders UI based on them.
// Functional component that greets the user by name
function Welcome(props) {
return (
<div>
<h2>Hello, {props.name}!</h2>
<p>This is a Functional Component.</p>
</div>
);
}
export default Welcome;
Functional components are preferred in modern React because they are easier to write, test, and enhance with Hooks like useState and useEffect.
A Class Component is defined using ES6 classes and extends React.Component. It must include a render() method that returns JSX.
// Class component with the same behavior as the functional one
import React, { Component } from "react";
class WelcomeClass extends Component {
render() {
return (
<div>
<h2>Hello, {this.props.name}!</h2>
<p>This is a Class Component.</p>
</div>
);
}
}
export default WelcomeClass;
Class components were widely used before Hooks were introduced in React 16.8. They support lifecycle methods like componentDidMount() and componentDidUpdate().
You can render both components inside a single parent like App to compare their behavior.
// App component rendering both functional and class components
import React from "react";
import Welcome from "./Welcome";
import WelcomeClass from "./WelcomeClass";
function App() {
return (
<div>
<Welcome name="Akash" />
<WelcomeClass name="Akash" />
</div>
);
}
export default App;
| Feature | Functional Component | Class Component |
|---|---|---|
| Syntax | Simple function returning JSX | ES6 class extending React.Component |
| State | Uses Hooks (e.g., useState) |
Uses this.state |
| Lifecycle | Managed via Hooks (e.g., useEffect) |
Uses lifecycle methods (e.g., componentDidMount) |
| Performance | Lighter and generally faster | Heavier due to class overhead |
| Code Readability | Short and simple | More verbose |
When you render the App component with the code above, the browser will show:
Both components behave identically from the user’s point of view. The main difference is how they are written and how they manage state and lifecycle internally.
MyButton).useState and useEffect instead of class lifecycle methods in new code.WelcomeMessage functional component that accepts name and course as props and displays a greeting.WelcomeClass component using the class syntax with the same output.App.js and observe that they behave identically.Goal: Understand the syntax, structure, and behavior of both functional and class components in React, and know when to use each.