The Basics
What is React?
A JavaScript library for building user interfaces.
| Year | What happened |
|---|---|
| 2011 | Built at Facebook |
| 2013 | Open-sourced |
| Today | Instagram, Airbnb, Netflix, and almost every major tech company |
Imperative (vanilla JS): you tell the browser step by step what to do. Find this element. Create that element. Set the text. Attach it to the page.
Declarative (React): you describe what the page should look like. React handles the rest.
1. JSX
HTML inside JavaScript. No more document.createElement. You see what the page looks like just by reading the code.
π§βπ» Example
function App() {
return <h1>Hello World</h1>;
}Curly braces for JavaScript expressions inside JSX:
const name = "Ahmed";
return <h1>Hello, {name}</h1>;return <p>{2 + 2}</p>;Rules:
- One parent element (wrap in <div> or <>...</>)
- Use className instead of class
- Self-closing tags: <img />, <input />, <br />
// Wrong
return (
<h1>Title</h1>
<p>Text</p>
);
// Right
return (
<div>
<h1>Title</h1>
<p>Text</p>
</div>
);π Summary
| Task | Code |
|---|---|
| Return HTML | return <h1>Hello</h1> |
| Use JS in JSX | {expression} |
| CSS class | className="title" |
| Self-closing tag | <img /> |
| Wrap multiple elements | <div>...</div> or <>...</> |
2. Components
Functions that return JSX. Instead of one giant file, you break the page into pieces.
π§βπ» Example
Create a component:
function Hero() {
return <h1>Welcome to Bashi Academy</h1>;
}Use it like an HTML tag:
function App() {
return (
<div>
<Hero />
</div>
);
}Rules:
- PascalCase names: Hero, NavBar, UserCard
- Must return JSX
- Use like an HTML tag: <Hero />
π Summary
| Task | Code |
|---|---|
| Create component | function Hero() { return ... } |
| Use component | <Hero /> |
| Naming | PascalCase |
3. Import & Export
One component per file. Export it out, import it in.
π§βπ» Example
Default export (one per file):
Hero.jsx:
function Hero() {
return <h1>Welcome to Bashi Academy</h1>;
}
export default Hero;App.jsx:
import Hero from "./Hero";
function App() {
return (
<div>
<Hero />
</div>
);
}Named export (many per file):
utils.jsx:
export function Hero() {
return <h1>Welcome</h1>;
}
export function Footer() {
return <footer>Β© 2025</footer>;
}import { Hero, Footer } from "./utils";The difference:
| Type | Export | Import |
|---|---|---|
| Default | export default Hero | import Hero from "./Hero" |
| Named | export function Hero | import { Hero } from "./Hero" |
Default: one per file, no braces. Named: many per file, braces required.
π Summary
| Task | Code |
|---|---|
| Default export | export default Hero |
| Default import | import Hero from "./Hero" |
| Named export | export function Hero |
| Named import | import { Hero } from "./Hero" |
4. Props
Data passed from parent to child. One component, different data.
π§βπ» Example
Pass like HTML attributes:
<Card title="React" description="A UI library" />Receive as a function parameter:
function Card(props) {
return (
<div>
<h2>{props.title}</h2>
<p>{props.description}</p>
</div>
);
}Destructuring (cleaner):
function Card({ title, description }) {
return (
<div>
<h2>{title}</h2>
<p>{description}</p>
</div>
);
}Default values:
function Card({ title = "Untitled", description = "" }) {
return (
<div>
<h2>{title}</h2>
<p>{description}</p>
</div>
);
}If a prop is missing, the default kicks in.
π Summary
| Task | Code |
|---|---|
| Pass props | <Card title="React" /> |
| Receive props | function Card(props) |
| Destructure | function Card({ title }) |
| Default value | { title = "Untitled" } |
| Use in JSX | {title} |
5. Conditional Rendering
Show the right thing at the right time. Logged in or logged out. Loading or loaded.
π§βπ» Example
Ternary (show A or B):
<button>{isOn ? "ON" : "OFF"}</button>&& operator (show or hide):
{isLoggedIn && <p>Welcome back!</p>}If isLoggedIn is true, the message shows. If false, nothing renders.
π Summary
| Task | Code |
|---|---|
| Show A or B | {condition ? <A /> : <B />} |
| Show or hide | {condition && <A />} |
6. Lists
Real apps show lists: products, users, messages. Turn an array into elements on screen.
π§βπ» Example
.map() over an array, return JSX for each item:
const names = ["Ahmed", "Faadumo", "Yusuf"];
return (
<ul>
{names.map((name) => (
<li key={name}>{name}</li>
))}
</ul>
);The key prop: React needs a unique key on each item to track what changed. Use an id, not the array index.
Array of objects:
const students = [
{ id: 1, name: "Ahmed", course: "React" },
{ id: 2, name: "Faadumo", course: "Python" },
];
return (
<ul>
{students.map((student) => (
<StudentCard key={student.id} name={student.name} course={student.course} />
))}
</ul>
);π Summary
| Task | Code |
|---|---|
| Render a list | array.map((item) => <li>{item}</li>) |
| Add a key | key={item.id} |
| Pass object as props | <Card key={item.id} name={item.name} /> |
| What NOT to use as key | Array index |