React Components
React Components
What is Components?
React components are reusable and independent building blocks that define parts of a user interface. They let you split the UI into independent, reusable pieces that can be managed and developed separately.
A component is a JavaScript function or class that:
- Returns a piece of JSX (JavaScript XML).
- Represents part of the UI.
- Can manage its own state and handle events.
Types of React Components
There are two main types of components in React:
a) Functional Components (modern & preferred)
Or using arrow functions:
b) Class Components (older way)
Why Use Components?
- Reusability: Build once, reuse everywhere.
- Maintainability: Small components are easier to manage.
- Separation of concerns: UI and logic stay organized.
Component with props
Props are used to receive data from parent components.
Component with State
State is data that changes over time and affects what a component displays.