What is a Component in React?
A component in React is a reusable UI block that maintains its own structure, state, and functions. React is built around components.
A component in React is a reusable UI block that manages its own structure, state, and functionality. React is built around components.
You can think of them as JavaScript functions that return JSX (HTML-like code).
React has two main types:
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
This component accepts `props` and returns JSX.
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
Although this still works, today we mainly use function components with React Hooks.
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
<Welcome name="Aram" />
`props` allow components to receive data dynamically.
A React component can have its own state, event handlers, and internal structure. In the next article, we'll talk about exactly this - **state and event handling**.
In the next part, we'll discuss component-local data (`state`) and how events are used.