Sobes.tech
Junior — Middle

How can a custom UI component be implemented in Jetpack Compose?

sobes.tech AI

Answer from AI

In Jetpack Compose, a custom UI component is created using a function annotated with @Composable. Such a function describes how the interface element should look and behave.

Example of a simple button component:

@Composable
fun MyButton(onClick: () -> Unit, text: String) {
    Button(onClick = onClick) {
        Text(text)
    }
}

To use the component, simply call it within another @Composable:

@Composable
fun Screen() {
    MyButton(onClick = { /* action on click */ }, text = "Press me")
}

Thus, creating UI components in Compose is a declarative description of the interface through functions, which simplifies development and maintenance of the code.