0Pricing
Jetpack Compose Academy · Pelajaran

Satu Sumber Kebenaran

Hindari state duplikat yang tidak sinkron.

Satu Sumber Kebenaran adalah pelajaran Jetpack Compose Academy gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Jetpack Compose Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

One Place for Each Fact

A single source of truth means every piece of state lives in exactly one spot. Everything else reads from it, never copies it.

The Danger of Duplicates

If two Composables each hold their own copy of the same value, they can drift apart. One updates, the other shows stale data. 😬

How Hoisting Helps

Hoisting naturally gives you a single owner. The parent holds the value; children only receive it, so there is nothing to fall out of sync.

Read From the Source

Children should always read from the hoisted value, not from a private copy. When the source changes, every reader updates together.

var query by remember { mutableStateOf("") }
SearchBar(value = query, onValueChange = { query = it })
Results(query = query)

Derive, Do Not Duplicate

Need a filtered or formatted version? Derive it from the source instead of storing a second copy that can go stale.

val matches = items.filter { it.contains(query) }

derivedStateOf for Heavy Work

When a derived value is expensive, wrap it in derivedStateOf so it recomputes only when its inputs truly change.

val matches by remember {
  derivedStateOf { items.filter { it.contains(query) } }
}

Two Inputs, One State

Want two fields editing the same data? Point both at the same hoisted state. They share one truth and stay perfectly in sync.

Avoid the Mirror Trap

A common bug is copying a parameter into local state inside an effect. Now you have a mirror that silently goes out of date.

Lift, Do Not Mirror

If a child needs to change shared data, lift the state to a common parent instead of duplicating it. One owner, many readers.

Truth and Testing

A single source makes bugs obvious: there is only one place a value can be wrong, so you know exactly where to look. ✅

The Golden Rule

State should be owned once and shared by reference. Duplicate state is the root of most sync bugs in Compose.

Quick Check

Why is keeping a single source of truth important?

Recap

Keep each fact in one place, read or derive from it, and never mirror it. One source of truth means no drift. 🚀

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Satu Sumber Kebenaran” gratis?

Ya — teks lengkap “Satu Sumber Kebenaran” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Jetpack Compose Academy, upgrade ke CoddyKit PRO. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Satu Sumber Kebenaran”?

Hindari state duplikat yang tidak sinkron. Kamu berlatih Jetpack Compose Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Jetpack Compose Academy?

Tidak diperlukan pengalaman sebelumnya. Jetpack Compose Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.

Berapa lama pelajaran “Satu Sumber Kebenaran” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Jetpack Compose Academy ini?

Ya. Setiap pelajaran Jetpack Compose Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Composable Stateful vs Stateless
  2. Mengangkat State dengan value & onValueChange
  3. Satu Sumber Kebenaran
  4. Slot State Compose & Lambda Konten
← Kembali ke Jetpack Compose Academy