Jedno źródło prawdy
Unikaj zduplikowanego i rozjeżdżającego się stanu
Jedno źródło prawdy to bezpłatna lekcja Jetpack Compose Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Jetpack Compose Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Jetpack Compose Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🚀
Często zadawane pytania
Czy lekcja „Jedno źródło prawdy” jest bezpłatna?
Tak — pełny tekst „Jedno źródło prawdy” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Jetpack Compose Academy, przejdź na CoddyKit PRO. Kurs Jetpack Compose Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Jedno źródło prawdy”?
Unikaj zduplikowanego i rozjeżdżającego się stanu Ćwiczysz Jetpack Compose Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Jetpack Compose Academy?
Nie wymagamy żadnego doświadczenia. Jetpack Compose Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Jedno źródło prawdy”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Jetpack Compose Academy?
Tak. Każda lekcja Jetpack Compose Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Stanowe i bezstanowe funkcje Composable
- Podnoszenie stanu za pomocą value i onValueChange
- Jedno źródło prawdy
- Sloty stanu Compose i lambdy zawartości