1

Consider the following code:

    @Composable
    private fun SomeComposable() {
        val lambda = {
            Button(onClick = {}) {
                Text("hello")
            }
        }
    }

That gives me an error on the Button with

@Composable invocations can only happen from the context of a @Composable function

So, how can I create a lambda for a composable? (I want to pass this around later on to another component).

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

1 Answers1

4

You can do it as

val lambda = @Composable {
    Button(onClick = {}) {
        Text("hello")
    }
}

Composable functions are like suspend functions you need to call them inside @Composable annotation

What are differents between Composable function and normal function in Android?

Thracian
  • 43,021
  • 16
  • 133
  • 222