What are React components?
In React, a component is basically a reusable building block of UI.
Itβs a JavaScript function or class that returns JSX (JavaScript XML), which describes how a part of the user interface should look and behave.
Key Points:
- Reusable β You can use a component in multiple places without rewriting code.
- Independent β Each component manages its own structure, logic, and style.
- Composable β Components can be combined together to build complex UIs.
- Maintainable β Helps keep code modular, clean, and easier to debug.
Types of Components in React
Functional Components (Modern & Preferred)
- A simple JavaScript function that returns JSX.Can use React Hooks (like
useState,useEffect) for state and lifecycle.
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}Class Components (Older way)
- Defined using ES6 classes.Use
this.stateand lifecycle methods (componentDidMount, etc.).
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}Example: Composing Components
function Header() {
return <h1>My Website</h1>;
}
function Footer() {
return <p>Β© 2025 My Company</p>;
}
function App() {
return (
<div>
<Header />
<p>Welcome to my site!</p>
<Footer />
</div>
);
}
Here, App is made up of Header and Footer components.
β
In short:
React components are the heart of React development β they let you split the UI into independent, reusable parts that can be managed and updated easily.
No comments yet! You be the first to comment.
