Skip to content

Commit d960174

Browse files
committed
Done react basic docs
1 parent eb1761b commit d960174

File tree

9 files changed

+992
-1
lines changed

9 files changed

+992
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "React",
3+
"position": 6,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Master the fundamentals of React.js. Learn about components, JSX, props, and state to build modern, interactive web applications."
7+
}
8+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
sidebar_position: 6
3+
title: "Hooks: Powering Up with useState"
4+
sidebar_label: "6. The useState Hook"
5+
description: "Learn how to make your components interactive by managing state with the useState hook."
6+
---
7+
8+
Imagine you are building a **Counter** app. You have a button that says "Click Me," and you want a number on the screen to go up every time you click it.
9+
10+
In plain JavaScript, you would have to find the HTML element and manually change its text. In React, we use the `useState` hook to tell React: *"Hey, keep track of this number, and whenever it changes, update the screen for me automatically!"*
11+
12+
## 🤔 What is a Hook?
13+
14+
Hooks are special functions that let you "hook into" React features. Think of them as **Power-Ups** for your components.
15+
* **Regular Function:** Just displays data.
16+
* **Function with Hooks:** Can remember things, perform actions, and react to user input.
17+
18+
## The Anatomy of useState
19+
20+
When you use `useState`, it always gives you back **two things** in an array:
21+
22+
1. **The Current State:** The current value stored in memory (e.g., `0`).
23+
2. **The Setter Function:** A special function used to update that value (e.g., `setCount`).
24+
25+
```javascript
26+
const [count, setCount] = useState(0);
27+
```
28+
29+
* `count` is the value.
30+
* `setCount` is the remote control to change that value.
31+
* `0` is the starting point (Initial State).
32+
33+
## Live Interactive: Your First State
34+
35+
Try clicking the button below! Watch how React instantly re-renders the component every time the `count` changes.
36+
37+
```jsx live
38+
function App() {
39+
// 1. Import (implicitly) and initialize state
40+
// We start at 0
41+
const [count, setCount] = React.useState(0);
42+
43+
return (
44+
<div style={{
45+
textAlign: 'center',
46+
padding: '20px',
47+
border: '2px solid #61dafb',
48+
borderRadius: '15px',
49+
backgroundColor: '#282c34',
50+
color: 'white'
51+
}}>
52+
<h2>The Click-O-Meter</h2>
53+
<h1 style={{ fontSize: '4rem', margin: '10px 0' }}>{count}</h1>
54+
55+
<button
56+
onClick={() => setCount(count + 1)}
57+
style={{
58+
padding: '10px 20px',
59+
padding: '5px',
60+
fontSize: '1.2rem',
61+
backgroundColor: '#61dafb',
62+
border: 'none',
63+
borderRadius: '5px',
64+
cursor: 'pointer',
65+
fontWeight: 'bold'
66+
}}
67+
>
68+
Increase Count +
69+
</button>
70+
71+
<button
72+
onClick={() => setCount(count - 1)}
73+
style={{
74+
padding: '10px 20px',
75+
padding: '5px',
76+
fontSize: '1.2rem',
77+
backgroundColor: '#ff6b6b',
78+
border: 'none',
79+
borderRadius: '5px',
80+
cursor: 'pointer',
81+
fontWeight: 'bold',
82+
marginLeft: '10px'
83+
}}
84+
>
85+
Decrease Count -
86+
</button>
87+
</div>
88+
);
89+
}
90+
91+
```
92+
93+
## The Golden Rule of State
94+
95+
**Never change state directly:**
96+
* `count = count + 1` (React won't notice, and the screen won't update).
97+
* `setCount(count + 1)` (React notices, updates the memory, and refreshes the UI).
98+
99+
:::tip Want to dive deeper?
100+
101+
`useState` is just the beginning! React has many other hooks like `useEffect` (for fetching data) and `useContext` (for global themes).
102+
103+
👉 **Explore the full Hook library:** For a detailed breakdown of all React Hooks, check out our [Comprehensive Hooks Guide](https://codeharborhub.github.io/tutorial/category/hooks).
104+
105+
:::
106+
107+
## Summary Checklist
108+
109+
* [x] I know that Hooks start with the word `use`.
110+
* [x] I understand that `useState` returns a value and a function to change it.
111+
* [x] I know that I must use the **Setter Function** to update the UI.
112+
* [x] I successfully built a working counter!

absolute-beginners/frontend-beginner/react/index.mdx

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
sidebar_position: 1
3+
title: "Why React? The Smart Way to Build"
4+
sidebar_label: "1. Intro to React"
5+
description: "A beginner's guide to why React is the world's favorite tool for building websites."
6+
---
7+
8+
Welcome to the **React** era! If you've already learned HTML, CSS, and JavaScript, you might be wondering: *"Why do I need a library? Can't I just use plain JavaScript?"*
9+
10+
The answer is: **Efficiency.**
11+
12+
In plain JavaScript, if you wanted to update a user's notification count, you would have to manually find the element, change the text, and make sure everything else on the page didn't break. In React, you just change the **Data**, and React magically updates the **UI** for you.
13+
14+
:::note
15+
If HTML is the **Skeleton** and JavaScript is the **Brain**, then **React** is the **Architect**.
16+
17+
React is a JavaScript library created by Facebook to make building user interfaces (UIs) easier, faster, and more organized. Instead of telling the browser every tiny step to take, you just describe what you want the screen to look like.
18+
:::
19+
20+
## The Mental Shift: How React Thinks
21+
22+
React acts like a **Smart Assistant**. Instead of you giving "Step-by-Step" instructions (Imperative), you just describe what you want the final result to look like (Declarative).
23+
24+
### The Visualization: Traditional vs. React
25+
26+
```mermaid
27+
graph TD
28+
subgraph "Traditional Way (Harder)"
29+
A[Data Changes] --> B[Find <br /> HTML Element]
30+
B --> C[Manually <br /> Update Text]
31+
C --> D[Check if <br /> other parts broke]
32+
end
33+
34+
subgraph "React Way (Smarter)"
35+
E[Data Changes] --> F{React Virtual DOM}
36+
F -->|Only updates <br /> what changed| G[UI Updates <br /> Instantly]
37+
end
38+
```
39+
40+
## The "Big Three" Features
41+
42+
Why did React win the "Web Library Wars"? Because of these three superpowers:
43+
44+
### 1. Components (The LEGO Mindset)
45+
46+
In React, you don't build "Pages." You build **Components**. A Navbar is a component. A Button is a component. You build them once and reuse them everywhere.
47+
48+
* **Benefit:** Fix a bug in the "Button" component, and it's fixed across your entire website instantly!
49+
50+
:::info 🧱 The Lego Mindset: Components
51+
52+
The biggest shift in React is thinking in **Components**.
53+
54+
Instead of one giant `index.html` file, you build small, independent pieces of code. You can reuse these pieces anywhere, just like LEGO bricks.
55+
56+
```mermaid
57+
graph TB
58+
subgraph "Your Website"
59+
A[Navbar]
60+
B[Sidebar]
61+
C[Main Feed]
62+
D[Footer]
63+
end
64+
65+
C --> E[Post Card]
66+
C --> F[Post Card]
67+
C --> G[Post Card]
68+
69+
E --> H[Like Button]
70+
E --> I[Comment Box]
71+
```
72+
:::
73+
74+
### 2. The Virtual DOM (The Fast Copy)
75+
76+
React keeps a lightweight "Ghost Copy" of your website in its memory.
77+
78+
* When something changes, React compares the "Ghost Copy" with the "Real Page."
79+
* It finds the **exact** spot that changed and only updates that tiny piece.
80+
* **Analogy:** If you get a haircut, React doesn't replace your whole body; it just updates your hair!
81+
82+
:::info The Virtual DOM (in other way)
83+
React keeps a "hidden copy" of your website in its memory. This is called the **Virtual DOM**.
84+
85+
When something changes, React follows these steps:
86+
1. It updates the **Virtual DOM** (which is lightning fast).
87+
2. It compares the Virtual DOM to the **Real DOM** (the one the user sees).
88+
3. It **only** changes the tiny piece that is different.
89+
90+
```mermaid
91+
graph TD
92+
A[Data Changes] --> B[Update Virtual DOM]
93+
B --> C[Compare with Real DOM]
94+
C --> D{What changed?}
95+
D -->|Only the Button| E[Update Button Only]
96+
D -->|Whole Page| F[Old Way: Refresh Everything]
97+
style E fill:#00d8ff,stroke:#333,stroke-width:2px,color:#333
98+
```
99+
100+
:::
101+
102+
### 3. Declarative UI (The Menu)
103+
104+
React is like a restaurant. You look at the menu and say, *"I want a Burger."* You don't go into the kitchen and tell the chef how to flip the patty. You describe the **State** (the burger), and React serves it.
105+
106+
## When should you use React?
107+
108+
You don't need React for a simple one-page resume. But you **definitely** need it for:
109+
110+
* **Social Media:** (Like/Comment counts updating live).
111+
* **Dashboards:** (Graphs and data changing in real-time).
112+
* **E-commerce:** (Adding items to a cart without refreshing the page).
113+
114+
## Why Beginners Love React
115+
116+
1. **Speed:** Your apps feel "snappy" because they don't refresh the whole page.
117+
2. **Reusability:** Write a `Button` component once, use it 100 times.
118+
3. **Job Demand:** Almost every modern tech company uses React. Learning this opens doors to professional careers.
119+
120+
121+
:::tip Vocabulary Check
122+
**SPA (Single Page Application):** A website that feels like a desktop app because it never refreshes the page while you click around. React is the king of SPAs!
123+
:::
124+
125+
## Quick Knowledge Check
126+
127+
| Feature | Plain JS (Vanilla) | React.js |
128+
| --- | --- | --- |
129+
| **Updates** | Slow (Re-renders whole sections) | Fast (Only updates changed parts) |
130+
| **Logic** | Manual (You do everything) | Automated (React handles the DOM) |
131+
| **Code** | Can get messy (Spaghetti) | Organized (Components) |
132+
133+
## Summary Checklist
134+
135+
* [x] I understand that React is for building **User Interfaces (UI)**.
136+
* [x] I know that **Components** are reusable building blocks.
137+
* [x] I understand the **Virtual DOM** makes websites feel fast.
138+
* [x] I'm ready to stop writing "Pages" and start building "Components."
139+
140+
:::tip Fun Fact / info
141+
* React is a free and open-source JavaScript library for building user interfaces (UIs), particularly for single-page applications. It is developed and maintained by Meta (formerly Facebook) and a large community of developers and companies.
142+
* React was created by **Jordan Walke**, a software engineer at Facebook. It was first used on Facebook’s Newsfeed in 2011 and later on Instagram!
143+
:::

0 commit comments

Comments
 (0)