Home > Software design >  How to access LocalComponent.current in Composable Buttons onClick?
How to access LocalComponent.current in Composable Buttons onClick?

Time:02-08

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

CodePudding user response:

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()
})
  •  Tags:  
  • Related