0Pricing
Jetpack Compose Academy · Lektion

Single Source of Truth

Vermeiden Sie doppelten und auseinanderlaufenden State.

Single Source of Truth ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🚀

Häufig gestellte Fragen

Ist die Lektion „Single Source of Truth“ kostenlos?

Ja — der vollständige Text von „Single Source of Truth“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Single Source of Truth“?

Vermeiden Sie doppelten und auseinanderlaufenden State. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?

Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Single Source of Truth“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?

Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Stateful und stateless Composables
  2. State mit value und onValueChange hoisten
  3. Single Source of Truth
  4. Compose-State-Slots und Content-Lambdas
← Zurück zu Jetpack Compose Academy