React is a popular JavaScript library (created by Facebook) for building fast, dynamic, and reusable user interfaces using a component-based architecture.
SPA Friendly Component-Based Virtual DOM
JSX lets you write HTML-like code inside JavaScript. React then converts JSX into regular JavaScript using React.createElement().
// JSX: writing UI using HTML-like syntax inside JavaScript
const element = <h1>Hello, React!</h1>;
Behind the scenes, React converts the JSX into:
// React automatically compiles JSX to this JavaScript form
const element = React.createElement("h1", null, "Hello, React!");
Both snippets create the same React element that represents an <h1> heading saying "Hello, React!". JSX is just a more human-friendly way to write this.
React apps are made of components. Each component is a function (or class) that returns UI elements. Components make your UI reusable and easier to maintain.
// Functional components used to build small pieces of UI
function Welcome() {
return <h2>Welcome to React!</h2>;
}
function App() {
return (
<div>
<Welcome />
<p>This is rendered by React.</p>
</div>
);
}
The App component renders a heading from Welcome and a paragraph below it. React combines both components to build the final UI on the page.
Welcome), not lowercase. React treats lowercase names as normal HTML tags.Props allow components to receive data from their parents. They make components flexible and reusable because you can pass different values each time.
// Using props to pass dynamic data into a component
function Greeting(props) {
return <h3>Hello, {props.name}!</h3>;
}
function App() {
return (
<div>
<Greeting name="Aarav" />
<Greeting name="Diya" />
</div>
);
}
The page will show two lines:
Hello, Aarav!Hello, Diya!The same Greeting component is reused with different name values.
State is used to store data that can change over time, like counters or form input. When state changes, React automatically re-renders the component to show the new UI.
// useState hook to add reactive state to a functional component
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click Me
</button>
</div>
);
}
Initially, the text shows You clicked 0 times. Every time you press Click Me, the count value increases by 1, and the text updates.
setCount(count + 1) to setCount(count + 2) and observe how the counter increases by 2 each time instead of 1.React needs a root element in your HTML file. Your React app is mounted inside this element.
<!-- Root element inside index.html where React will render the app -->
<div id="root"></div>
Then, you attach your React app to this root element using JavaScript:
// Entry point: connect React components to the root DOM node
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
After this setup, whatever your App component returns will be rendered inside the <div id="root"> element on the actual web page.
useEffect for side-effects and data fetching.<div> or <React.Fragment>).