Concept Explanation
React is the most popular way to build websites today. It was created by Meta (Facebook) and introduced the concept of the Virtual DOM.
Instead of touching the real (slow) browser DOM, React creates a lightweight copy in memory. It calculates what changed and only updates the tiny parts that need it.
Syntax or Core Structure
A Simple React Component (JSX)
function WelcomeCard({ name }) {
return (
<div className="card">
<h1>Hello, {name}!</h1>
<button onClick={() => alert("Clicked!")}>Click Me</button>
</div>
);
}
Key Features
- JSX: A way to write HTML-looking code directly inside JavaScript.
- Hooks: Special functions like
useStatethat let you handle data and "Actions" inside components. - Props: Data passed from a parent component to a child.
Best Practices
- One-Way Data Flow: Data should always flow down from parent to child.
- Keep Components Pure: A component should always look the same if you give it the same data.
- Hooks at the Top: Never call a Hook inside a loop, condition, or nested function.
Practice Exercises
- The JSX Converter: Take a simple HTML snippet (
divwith anh1andp) and rewrite it as a React component. - The State Mentalist: Imagine a "Counter" app. List the steps required to update the number on screen using
useState. - The Loop: Write a React snippet that maps over an array of names and renders a
<li>for each.