-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate-lifting-vs-lifting-logic.js
More file actions
114 lines (102 loc) · 2.81 KB
/
state-lifting-vs-lifting-logic.js
File metadata and controls
114 lines (102 loc) · 2.81 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* 🧠 Concept: State Lifting vs Lifting Logic in React
*
* ---------------------------------------------------------
* 🧾 Definition:
*
* 🔹 **State Lifting**:
* - Move shared state **up** to a common parent component.
* - Ensures consistent data flow between sibling components.
*
* 🔹 **Lifting Logic**:
* - Move only the **functionality or behavior** (logic) up or into reusable functions/hooks.
* - The state can still reside lower, but logic is decoupled.
*
* ---------------------------------------------------------
* ✅ Scenario: Two child components share a counter
*/
import { useState } from "react";
// 🔸 State Lifting Example
function ParentWithLiftingState() {
const [count, setCount] = useState(0);
return (
<div>
<ChildA count={count} setCount={setCount} />
<ChildB count={count} />
</div>
);
}
function ChildA({ count, setCount }) {
return (
<div>
<h3>Child A</h3>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
function ChildB({ count }) {
return (
<div>
<h3>Child B</h3>
<p>Count from sibling: {count}</p>
</div>
);
}
/**
* 🔍 State is lifted to the parent to **share between siblings**.
*
* 🧩 Problem: Repeating logic in each component can bloat code.
*
* ---------------------------------------------------------
* 🔄 Now let's lift just the logic (reusable behavior)
*/
// 🔸 Logic Lifting with Custom Hook
function useCounterLogic(initial = 0) {
const [count, setCount] = useState(initial);
const increment = () => setCount((prev) => prev + 1);
return { count, increment };
}
// Each component maintains its own state, but reuses logic
function AComponent() {
const { count, increment } = useCounterLogic();
return (
<div>
<h3>Logic Hook - AComponent</h3>
<button onClick={increment}>A Increment: {count}</button>
</div>
);
}
function BComponent() {
const { count, increment } = useCounterLogic();
return (
<div>
<h3>Logic Hook - BComponent</h3>
<button onClick={increment}>B Increment: {count}</button>
</div>
);
}
/**
* 🔍 Logic is lifted into a custom hook.
* 🔄 Each component has **independent state**, but **shared behavior**.
*/
/**
* ---------------------------------------------------------
* 🧵 Summary:
*
* 🔹 **State Lifting**
* - When multiple components need access to the same state.
* - State resides in common ancestor.
*
* 🔹 **Lifting Logic**
* - When logic (e.g., counter, fetching, toggling) can be reused.
* - Helps reduce duplication and increases code reuse.
*
* ✅ Best Practice:
* - Use state lifting for **shared state**.
* - Use logic lifting (custom hooks) for **shared behavior**.
*/
export {
ParentWithLiftingState,
AComponent,
BComponent
};