React ን ጥልቅ እናውቅ! 🔥
Go deeper into React — effects, rendering lists, and conditional UI!
useEffect — Component ሲጫን፣ State ሲቀየር ወይም ሲዘጋ አንድ ነገር ለማስሮጥ ይጠቅማል! API call, Timer, Event listener...
useEffect runs code after render — perfect for API calls, timers, and subscriptions.
Syntax: useEffect(() => { /* code */ }, [dependencies])
— [] = አንድ ጊዜ ብቻ (on mount)
— [value] = value ሲቀየር ብቻ
— nothing = ሁሌ render ሲሆን
The dependency array controls when the effect runs.
⏱️ Component Lifecycle:
Component page ላይ ሲጫን — useEffect(fn, []) ይሰራል
Component first appears on screen
State ወይም Props ሲቀየር — useEffect(fn, [dep]) ይሰራል
Re-renders when state or props change
Component page ላይ ሲወጣ — cleanup function ይሰራል
Cleanup runs when component is removed
import { useState, useEffect } from 'react'; function Timer() { const [seconds, setSeconds] = useState(0); const [running, setRunning] = useState(false); useEffect(() => { if (!running) return; // running ካልሆነ ቁም // ሁሉም ሰከንድ +1 ይጨምር const interval = setInterval(() => { setSeconds(s => s + 1); }, 1000); // Cleanup — interval ያስቁም return () => clearInterval(interval); }, [running]); // running ሲቀየር ብቻ ይሰራ return ( <div> <h2>{seconds}s</h2> <button onClick={() => setRunning(!running)}> {running ? 'Stop' : 'Start'} </button> </div> ); }
🎮 Demo — Timer (useEffect ምሳሌ):
This uses the same logic as useEffect with setInterval!
React ውስጥ Array ን .map() ተጠቅሞ JSX ዝርዝር ማሳየት ይቻላል! ሁሉም item key prop ያስፈልገዋል!
Use .map() to render lists in React. Every item MUST have a unique key prop!
key prop ግዴታ ነው! React ዝርዝር ውስጥ ለውጥ ሲሆን ለማወቅ key ይጠቀማል። key unique መሆን አለበት!
key is required! React uses it to track which items changed. Must be unique.
function LessonList() { const lessons = [ { id: 1, title: 'HTML Basics', done: true }, { id: 2, title: 'CSS Flexbox', done: true }, { id: 3, title: 'JavaScript', done: true }, { id: 4, title: 'React Basics', done: false }, ]; return ( <ul> {lessons.map(lesson => ( <li key={lesson.id}> {/* key ግዴታ! */} {lesson.done ? '✅' : '⏳'} {lesson.title} </li> ))} </ul> ); }
🎮 Demo — Live List Rendering:
Add items to the array — React renders each with a unique key!
React ውስጥ ሁኔታ ሲኖር ልዩ ልዩ UI ማሳየት ይቻላል! if, &&, ternary (?:) ሁሉም ይሰራሉ!
Show different UI based on conditions using if, &&, or the ternary operator!
function UserStatus({ isLoggedIn, isAdmin, name }) { // 1. if statement — ሙሉ block if (!isLoggedIn) { return <p>እባክዎ Login ያድርጉ</p>; } return ( <div> {/* 2. Ternary — አጭር if/else */} <h2> {isAdmin ? '👑 Admin' : '👤 User'}: {name} </h2> {/* 3. && — ሁኔታ እውነት ከሆነ ብቻ */} {isAdmin && ( <button>🗑️ Delete Users</button> )} <p>ሰላም! ትምህርት ጀምር</p> </div> ); }
🎮 Demo — Conditional Rendering: ሁኔታ ቀይር → UI ይቀያየር!
Change the state — watch the UI update conditionally!
useEffect(() => { }, [deps]) — API call, Timer, Event listener ለማስሮጥ።
Runs after render. [] = once, [val] = when val changes.
useEffect ውስጥ return () => { } ሲጻፍ Component ሲወጣ ይሰራል — Timer, listener ለማቆም።
Return a cleanup function to avoid memory leaks.
Array.map() ተጠቅሞ JSX ዝርዝር ማሳየት — ሁሉም item key prop ያስፈልገዋል!
Use .map() to render lists. key prop is required and must be unique.
if return | condition && <JSX> | condition ? <A> : <B>
Three ways: early return, && operator, ternary operator.