Skip to content

Commit 5e75a2e

Browse files
committed
Add basic useEffect example and update showcase
Introduces a simple useEffect example (01_BasicLog) that logs count changes after render. Updates the useEffect showcase to display this example and comments out the useState showcase in App.tsx.
1 parent 3cb8ce9 commit 5e75a2e

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

β€Žsrc/App.tsxβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import './App.css'
2+
import UseEffectShowcase from './hooks/useEffect/useEffect'
23
import UseStateShowcase from './hooks/useState/UseState'
34

45
function App() {
56

67
return (
78
<>
8-
<UseStateShowcase />
9+
{/* <UseStateShowcase /> */}
10+
<UseEffectShowcase />
911
</>
1012
)
1113
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Example: BasicLog
3+
*
4+
* Explanation:
5+
* - Simplest useEffect example: log after render.
6+
* - Demonstrates that effects run *after* paint.
7+
* - Useful for learning that useEffect is asynchronous relative to rendering.
8+
*/
9+
import { useEffect, useState } from "react";
10+
11+
export default function BasicLog() {
12+
const [count, setCount] = useState(0);
13+
14+
useEffect(() => {
15+
console.log("Effect: count changed β†’", count);
16+
}, [count]);
17+
18+
return (
19+
<div>
20+
<p>Count = {count}</p>
21+
<button onClick={() => setCount(c => c + 1)}>+1</button>
22+
</div>
23+
);
24+
}

β€Žsrc/hooks/useEffect/useEffect.tsxβ€Ž

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Each demo is isolated in its own file inside `examples/`.
77
*/
88

9-
// import BasicLog from "./examples/BasicLog";
9+
import BasicLog from "./examples/01_BasicLog";
1010
// import EffectCleanup from "./examples/EffectCleanup";
1111
// import DependencyArray from "./examples/DependencyArray";
1212
// import WindowResizeListener from "./examples/WindowResizeListener";
@@ -39,43 +39,43 @@ export default function UseEffectShowcase() {
3939
<section>
4040
<h1>useEffect β€” Curated Examples</h1>
4141

42-
{/* <Demo title="1) BasicLog β€” log after render">
42+
<Demo title="1) BasicLog β€” log after render">
4343
<BasicLog />
4444
</Demo>
4545

46-
<Demo title="2) EffectCleanup β€” cleanup on unmount">
46+
{/* <Demo title="2) EffectCleanup β€” cleanup on unmount">
4747
<EffectCleanup />
48-
</Demo>
48+
</Demo> */}
4949

50-
<Demo title="3) DependencyArray β€” [] vs deps vs no deps">
50+
{/* <Demo title="3) DependencyArray β€” [] vs deps vs no deps">
5151
<DependencyArray />
52-
</Demo>
52+
</Demo> */}
5353

54-
<Demo title="4) WindowResizeListener β€” global event subscription">
54+
{/* <Demo title="4) WindowResizeListener β€” global event subscription">
5555
<WindowResizeListener />
56-
</Demo>
56+
</Demo> */}
5757

58-
<Demo title="5) FetchData β€” async request with cleanup">
58+
{/* <Demo title="5) FetchData β€” async request with cleanup">
5959
<FetchData />
60-
</Demo>
61-
60+
</Demo> */}
61+
{/*
6262
<Demo title="6) MultipleEffects β€” separate concerns">
6363
<MultipleEffects />
64-
</Demo>
64+
</Demo> */}
6565

66-
<Demo title="7) EffectVsLayoutEffect β€” timing differences">
66+
{/* <Demo title="7) EffectVsLayoutEffect β€” timing differences">
6767
<EffectVsLayoutEffect />
68-
</Demo>
68+
</Demo> */}
6969

70-
<Demo title="8) DebouncedInput β€” debounce with setTimeout">
70+
{/* <Demo title="8) DebouncedInput β€” debounce with setTimeout">
7171
<DebouncedInput />
72-
</Demo>
72+
</Demo> */}
7373

74-
<Demo title="9) IntervalCounter β€” timer with cleanup">
74+
{/* <Demo title="9) IntervalCounter β€” timer with cleanup">
7575
<IntervalCounter />
76-
</Demo>
76+
</Demo> */}
7777

78-
<Demo title="10) RaceConditionFix β€” async abort pattern">
78+
{/* <Demo title="10) RaceConditionFix β€” async abort pattern">
7979
<RaceConditionFix />
8080
</Demo> */}
8181
</section>

0 commit comments

Comments
Β (0)