Composables ذات الحالة مقابل عديمة الحالة
افصل المنطق عن العرض
Composables ذات الحالة مقابل عديمة الحالة درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Two Kinds of Composables
Every Composable is either stateful (it holds its own data) or stateless (it just renders what you pass in). Knowing which is which shapes your whole app.
What Stateful Means
A stateful Composable remembers something across recompositions, usually with remember. It owns and changes its own data.
fun Counter() {
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) { Text("Count: " + count) }
}What Stateless Means
A stateless Composable holds no state of its own. It only reads its parameters and draws, so the same inputs always give the same output. 🎯
fun CounterDisplay(count: Int) {
Text("Count: " + count)
}Why Stateless Wins
Stateless Composables are easier to reuse: they fit any screen, in any state, because the caller decides what to show.
Easier to Preview
Because a stateless Composable just takes parameters, you can feed it sample data in @Preview and see every variation instantly.
@Preview
fun PreviewDisplay() {
CounterDisplay(count = 42)
}Easier to Test
Stateless parts are predictable: pass an input, assert the output. No hidden internal data means far simpler tests. ✅
The Trade-Off
Stateful Composables are convenient for quick demos, but they trap data inside. That makes them hard to share, preview, or control from outside.
Spotting the Difference
See a remember or mutableStateOf inside? It is stateful. See only parameters being read? It is stateless.
Separate Logic from Looks
A clean pattern: keep one thin stateful wrapper that owns data, and a stateless Composable that only handles how things look.
Composing the Two
The stateful wrapper feeds its data into the stateless one. Now the visual part stays pure while the state lives in just one place.
fun CounterScreen() {
var count by remember { mutableStateOf(0) }
CounterDisplay(count = count)
}A Mindset for Reuse
Default to stateless when you build components. Add state only at the spot that truly needs to own it, and no lower.
Quick Check
Which kind of Composable is the easiest to reuse and preview?
Recap
Stateful Composables own data; stateless ones just render parameters. Favor stateless components and keep state in one small wrapper. 🚀
الأسئلة الشائعة
هل درس «Composables ذات الحالة مقابل عديمة الحالة» مجاني؟
نعم — نص درس «Composables ذات الحالة مقابل عديمة الحالة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
ماذا ستتعلم في «Composables ذات الحالة مقابل عديمة الحالة»؟
افصل المنطق عن العرض تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟
لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «Composables ذات الحالة مقابل عديمة الحالة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟
نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- Composables ذات الحالة مقابل عديمة الحالة
- رفع الحالة باستخدام value وonValueChange
- مصدر واحد للحقيقة
- خانات حالة Compose وContent Lambdas