0Pricing
Kotlin Multiplatform Academy · درس

الحالة وإعادة التركيب في الواجهة المشتركة

قيادة Compose باستخدام StateFlow المشترك

الحالة وإعادة التركيب في الواجهة المشتركة درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

UI Is a Function of State

In Compose your screen is drawn from state. When the state changes, Compose redraws only the parts that depend on it. You never mutate views by hand.

Recomposition Explained

That automatic redraw is called recomposition. Compose re-runs the affected composables with the new values and updates the screen for you.

Remember Local State

For state owned by a composable, use remember with mutableStateOf so the value survives recomposition instead of resetting every frame.

val count = remember { mutableStateOf(0) }

Read and Write State

Wrap the value in by to read and write it like a normal variable. Reading it subscribes the composable, so writing triggers a redraw.

var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
    Text("Count: " + count)
}

Hoist State Upward

Move state to the lowest common parent and pass values down. This hoisting keeps child composables stateless, reusable and easy to test.

@Composable
fun Counter(value: Int, onInc: () -> Unit) {
    Button(onClick = onInc) { Text("" + value) }
}

State Lives in Shared Code

Your real source of truth is the shared layer. A cross-platform StateFlow holds screen state once, far from any platform UI framework.

val uiState: StateFlow<ScreenState> = _state

Collect StateFlow in Compose

Bridge that flow into Compose with collectAsState. The composable observes it and recomposes whenever the shared state emits a new value.

val state by viewModel.uiState.collectAsState()
Text(state.title)

One Source of Truth

Because state flows from shared code into Compose, Android and iOS render the exact same screen from a single source of truth. No drift.

Avoid Side Effects in Composables

A composable can run many times, so never do work like network calls directly in its body. Keep it a pure description and push effects elsewhere.

LaunchedEffect for Side Work

When you must start a coroutine tied to the UI, use LaunchedEffect. It runs once per key and cancels automatically when the composable leaves.

LaunchedEffect(Unit) {
    viewModel.load()
}

Derived and Stable State

Compute values from existing state with derivedStateOf so they recalculate only when their inputs change, keeping recomposition cheap.

Quick Check

What keeps a value across recompositions?

Recap: State Drives the UI

You learned that state drives recomposition, how to remember and hoist it, and how shared StateFlow feeds Compose so both apps render one truth. 🔁

الأسئلة الشائعة

هل درس «الحالة وإعادة التركيب في الواجهة المشتركة» مجاني؟

نعم — نص درس «الحالة وإعادة التركيب في الواجهة المشتركة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «الحالة وإعادة التركيب في الواجهة المشتركة»؟

قيادة Compose باستخدام StateFlow المشترك تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «الحالة وإعادة التركيب في الواجهة المشتركة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تمكين Compose Multiplatform
  2. التخطيطات وModifiers وتنسيق الواجهة
  3. الحالة وإعادة التركيب في الواجهة المشتركة
  4. استضافة الواجهة المشتركة على iOS وDesktop
← العودة إلى Kotlin Multiplatform Academy