Fonte Única da Verdade
Evite estados duplicados e divergentes.
Fonte Única da Verdade é uma aula grátis de Jetpack Compose Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Jetpack Compose Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Jetpack Compose Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. 🚀
Perguntas Frequentes
A aula “Fonte Única da Verdade” é grátis?
Sim — o texto completo de “Fonte Única da Verdade” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Jetpack Compose Academy, atualize para CoddyKit PRO. O curso de Jetpack Compose Academy inclui 4 aulas no total.
O que vou aprender em “Fonte Única da Verdade”?
Evite estados duplicados e divergentes. Você pratica Jetpack Compose Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Jetpack Compose Academy?
Nenhuma experiência prévia é necessária. Jetpack Compose Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Fonte Única da Verdade”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Jetpack Compose Academy?
Sim. Cada aula de Jetpack Compose Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Composables com e sem Estado
- Elevando o Estado com value e onValueChange
- Fonte Única da Verdade
- Slots de Estado do Compose e Lambdas de Conteúdo