Props va komponent arxitekturasi
Katta
App.jsProps nima?
Props — ota komponentdan bolaga beriladigan readonly ma'lumot:
function GoalItem({ text, onDelete }) { return ( <View style={styles.item}> <Text>{text}</Text> <Button title="O'chirish" onPress={onDelete} /> </View> ); } // Ishlatish: <GoalItem text="React o'rganish" onDelete={() => deleteGoal(id)} />
- — ko'rsatiladigan matn
text - — funksiya ham prop bo'lishi mumkin (callback pattern)
onDelete
Komponentlarni bo'lish — nega?
Single Responsibility: har bir fayl bitta ish qilsin.
components/ GoalItem.js — bitta maqsad qatori GoalInput.js — kiritish maydoni + tugma screens/ GoalsScreen.js — ularni birlashtiradi
Afzalliklari:
- O'qish va debug oson
- Bir komponentni boshqa loyihada qayta ishlatish
- Jamoa bilan parallel ishlash
GoalInput misoli
// components/GoalInput.js function GoalInput({ onAddGoal }) { const [text, setText] = useState(''); function submit() { if (!text.trim()) return; onAddGoal(text); setText(''); } return ( <View style={styles.row}> <TextInput style={styles.input} value={text} onChangeText={setText} placeholder="Maqsad..." /> <Button title="Qo'shish" onPress={submit} /> </View> ); }
Input ichki state saqlaydi; tayyor matn faqat
onAddGoalState qayerda turishi kerak?
- Bir nechta bolaga kerak bo'lgan ma'lumot — eng yaqin otada
- Faqat bitta komponentga tegishli — shu komponentda
Goals ro'yxati
AppGoalsScreenGoalInputchildren prop
function Card({ children }) { return <View style={styles.card}>{children}</View>; } <Card> <Text>Ichidagi kontent</Text> </Card>
Xulosa
- Props — komponentlar orasidagi shartnoma
- Kichik komponentlar = toza kod
- Callback props (,
onAddGoal) — bola ota ga xabar beradionDelete - State joylashuvi — arxitektura ning kaliti
Keyingi darsda uzun ro'yxatlar: ScrollView va FlatList.