
Day 3: Understanding JSX and Components in React
Welcome back to our React Learning Series!, we already set up React, and created a portfolio page. Today, weβll dive into JSX and Components, the heart of React.
What is JSX?
JSX stands for JavaScript XML.
It allows us to write HTML-like code inside JavaScript, which React then transforms into actual DOM elements.
π Without JSX:
const element = React.createElement("h1", null, "Hello, React!");
π With JSX:
const element = <h1>Hello, React!</h1>;
Features of JSX:
- Looks like HTML but is actually JavaScript.
- Expressions in Curly Braces
{}
β You can use variables inside JSX.
const name = "Anand";
const element = <h2>Hello, {name}!</h2>;
Must return a single parent element β
return (
<div>
<h1>Title</h1>
<p>Description</p>
</div>
);
β You canβt return two sibling elements without a parent.
What are Components?
Components are independent, reusable pieces of UI.
Think of them like LEGO blocks β each block is small, but when combined, they create something bigger.
Types of Components in React
Functional Component (Modern & Preferred)
A Functional Component is a plain JavaScript function that accepts props (if needed) and returns JSX to display UI.
A simple JavaScript function that returns JSX.
- It does not need a class or
this
keyword. - Itβs the most common and modern way to write components.
function Welcome() {
return <h2>Welcome to React!</h2>;
}
export default Welcome;
Arrow Function Component
An Arrow Function Component is a functional component written with arrow function syntax (()=>{}
) to make code shorter and modern.
A shorter version of functional components using ES6 arrow function syntax.
- Cleaner and preferred for small components.
const Welcome = () => {
return <h2>Welcome to React!</h2>;
};
Class Component (Older, Less Used Now)
A Class Component is a component created using ES6 classes. It includes a render()
method and can use lifecycle methods, but is less common today because functional components + hooks are more powerful and simpler.
Before hooks(We will learn in React Hooks later) were introduced (React 16.8), developers used class components to handle state and lifecycle methods.
- They use the
class
keyword and extendReact.Component
. - Require
render()
method to return JSX.
import React, { Component } from "react";
class Welcome extends Component {
render() {
return <h2>Welcome to React!</h2>;
}
}
export default Welcome;
Quick Comparison
Feature | Functional Component | Arrow Function Component | Class Component |
---|---|---|---|
Syntax | function MyComp(){} | const MyComp = () => {} | class MyComp extends Component {} |
Uses this | β No | β No | β Yes |
Recommended Today | β Yes | β Yes (for short) | β No (legacy) |
Hooks Supported | β Yes | β Yes | β No (used lifecycle methods) |
Creating Your First Component
π Create a new file Header.jsx
in the src/components/
folder.
function Header() {
return (
<header>
<h1>My Portfolio</h1>
<p>Learning React step by step π</p>
</header>
);
}
export default Header;
π Import it in App.jsx
:
import Header from "./components/Header";
function App() {
return (
<div>
<Header />
</div>
);
}
export default App;
π Congratulations! You just created and used your first React component.
Why Use Components?
- Reusable β You can use the same component in multiple places.
- Readable β Small, separate files make your app easier to understand.
- Maintainable β Updating one component automatically updates everywhere itβs used.
Example:
function Button() {
return <button>Click Me</button>;
}
// Can be reused in multiple places:
<Button />
<Button />
Summary of Day 3
- JSX lets us write HTML-like syntax inside JavaScript.
- Components are the building blocks of a React app.
- We built our first component (Header) and used it in
App.jsx
. - React apps are simply a tree of components working together.