Compose Flashcards
(66 cards)
What is Compose used for?
Compose is used to develop UI in a declarative and flexible way instead of using XML.
How does Compose simplify UI development?
It simplifies UI development by making it easier to manage state, handle updates efficiently, and create reusable UI elements.
How are UI elements defined in Compose?
UI elements are defined as composable functions in Kotlin.
What is declarative UI?
Declarative UI describes what the UI should look like, and it automatically updates when the state changes. It makes UI development more efficient and eliminates the need for manual updates.
How is traditional UI different from declarative UI?
Traditional UI describes how the UI should be built, requiring manual updates when the state changes.
What is the remember keyword used for in Compose?
It is used to retain data across recompositions until the composable is removed.
What happens if you don’t use remember in a composable function?
Without remember, all variables inside a composable function would be created on every recomposition.
What is Recomposition in Compose?
Recomposition is the process of updating the UI when the state changes.
How does Recomposition improve efficiency in Compose?
Instead of recreating the entire UI, Compose only re-executes the parts that depend on the updated state, making UI updates more efficient and avoiding unnecessary recomputation.
What is state hoisting in Compose?
State hoisting means that a composable doesn’t manage its own state but instead receives the state and a way to update it from its parent.
What are the benefits of state hoisting?
It makes the composable stateless, more reusable, and easier to manage.
What are side effects in Compose?
Side effects indicate changes to the state or other app behaviors that happen outside the scope of a composable function.
What typically triggers side effects in Compose?
Side effects are typically triggered by user interactions or external events and can affect the state and UI.
What is CompositionLocal in Compose?
CompositionLocal is used to pass data down the UI tree without passing it as a parameter to every composable.
What are the benefits of CompositionLocal?
It helps reduce boilerplate code while keeping the data accessible only within the defined scope.
How do you use CompositionLocal in Compose?
- Create a CompositionLocal variable.
- Use CompositionLocalProvider to set a value for the CompositionLocal.
- Retrieve the value using .current inside a composable.
What is State Management in Compose?
Compose encourages unidirectional data flow, meaning the state flows down, events flow up, and the UI reacts to state changes.
How does unidirectional data flow help in Compose?
It ensures better state control, predictability, and easier debugging by keeping state changes centralized.
What is rememberCoroutineScope in Compose?
It provides a coroutine scope tied to the lifecycle of a composable, ensuring all launched coroutines are automatically canceled when the composable is destroyed.
Why use rememberCoroutineScope?
It helps manage coroutines safely within a composable, preventing memory leaks and unnecessary background tasks.
How do you convert a Flow or LiveData into a Compose state?
Use collectAsState for Flow and observeAsState for LiveData.
How do you convert regular data into a Compose state?
Use remember with mutableStateOf.
How do you transform asynchronous results or side effects into a Compose state?
Use produceState, which requires an initial value.
How do you derive a new state from an existing state in Compose?
Use derivedStateOf.