forked from yugank-rai/React.js_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL6(REACT-USE_Effect).js
More file actions
96 lines (82 loc) · 2.68 KB
/
L6(REACT-USE_Effect).js
File metadata and controls
96 lines (82 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* TOPIC: THE useEffect HOOK
* Source: Piyush Garg (React Tutorial Series)
* * WHAT IS useEffect?
* - It's a Hook that lets you perform "Side Effects" in functional components.
* - Side Effects include: Fetching data from an API, setting up a subscription,
* manually changing the DOM, or setting up timers (setTimeout/setInterval).
* - It "injects" logic into the Component Lifecycle (Mount, Update, Unmount). [00:03:11]
*/
import React, { useState, useEffect } from 'react';
function UseEffectExample() {
const [count, setCount] = useState(0);
const [otherCount, setOtherCount] = useState(50);
/**
* SYNTAX: useEffect(setupFunction, dependencyArray);
* * CASE 1: No Dependency Array
* Runs on every single render (mount + every update).
*/
useEffect(() => {
console.log("I run on EVERY render");
});
/**
* CASE 2: Empty Dependency Array []
* Runs ONLY ONCE when the component mounts (appears on screen).
* Perfect for API calls or starting a timer.
*/
useEffect(() => {
console.log("I only run ONCE (Mounting)");
}, []);
/**
* CASE 3: With Dependencies [count]
* Runs on mount + whenever 'count' changes.
* It will NOT run if 'otherCount' changes.
*/
useEffect(() => {
console.log("Count changed to:", count);
/**
* CLEANUP FUNCTION (The 'Return' Function)
* - React runs this before the component unmounts.
* - It also runs before re-running the effect due to a dependency change.
* - Used to clear timers, cancel API requests, or remove event listeners.
*/
return () => {
console.log("Cleaning up before next effect or unmount...");
};
}, [count]);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Update Count</button>
<p>Other Count: {otherCount}</p>
<button onClick={() => setOtherCount(otherCount + 1)}>Update Other</button>
</div>
);
}
/**
* PRACTICAL EXAMPLE: A TIMER
*/
function TimerComponent() {
const [time, setTime] = useState(0);
useEffect(() => {
// 1. Setup side effect (Interval)
const interval = setInterval(() => {
console.log("Adding new interval...");
setTime((prev) => prev + 1);
}, 1000);
// 2. Cleanup side effect
// Without this, intervals will stack up and crash the app!
return () => {
console.log("Clearing old interval...");
clearInterval(interval);
};
}, []); // Runs once on mount, cleans up on unmount
return <h1>Time: {time}s</h1>;
}
/**
* LIFECYCLE SUMMARY:
* 1. Mounting: Component enters the UI.
* 2. Updating: State or Props change -> Re-render.
* 3. Unmounting: Component leaves the UI.
*/
export default UseEffectExample;