Arsene Raul
Arsene Raul

Reputation: 66

How to access LocalComponent.current in Composable Buttons onClick?

I have a Button and I would like to access LocalComponents.current in its onClick

Button(onClick = {
    with(LocalRootComponent) { 
        current.doStuff()
    }
})

but IDE complains with

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

Upvotes: 0

Views: 92

Answers (1)

cd1
cd1

Reputation: 16534

You can access and store that object outside the onClick method, and then use that variable inside onClick , e.g.:

val rootComponent = LocalRootComponent.current

Button(onClick = {
    rootComponent.doStuff()
})

Upvotes: 2

Related Questions