Nivel 5
Nivel 5: state hoisting (flujo de datos unidireccional)
Traducción al español en progresoEl contenido detallado de esta lección aún se está traduciendo. Mientras tanto, se muestra en inglés.
Unidirectional Data Flow
UI components should be dumb. They receive data (`state`) and emit events (`callbacks`). They shouldn't change state themselves.
📚 Explicación
State hoisting means moving state up to the nearest common ancestor. Child components receive state as parameters and emit events via callbacks. This makes components reusable and testable.
Nota: La ejecución de código no está disponible para Android/Kotlin en el navegador. Usa Android Studio para ejecutar y probar la app.
// 1. Dumb Component (Stateless)@Composablefun SearchBar(query: String, // State comes downonQueryChanged: (String) -> Unit // Events go up) {TextField(value = query,onValueChange = onQueryChanged)}// 2. Smart Parent (Stateful)@Composablefun SearchScreen() {var query by remember { mutableStateOf("") }// The parent manages the state and passes it downSearchBar(query = query,onQueryChanged = { newText -> query = newText })}