0Pricing
Jetpack Compose Academy · Lesson

Single Source of Truth

Avoid duplicated, drifting state.

Single Source of Truth is a free Jetpack Compose Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Single Source of Truth” lesson free?

Yes — the full text of “Single Source of Truth” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “Single Source of Truth”?

Avoid duplicated, drifting state. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Single Source of Truth” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Stateful vs Stateless Composables
  2. Hoist State with value & onValueChange
  3. Single Source of Truth
  4. Compose State Slots & Content Lambdas
← Back to Jetpack Compose Academy