如何在可组合按钮 onClick 中访问 LocalComponent.current?

How to access LocalComponent.current in Composable Buttons onClick?

我有一个按钮,我想在其 onClick

中访问 LocalComponents.current
Button(onClick = {
    with(LocalRootComponent) { 
        current.doStuff()
    }
})

但是IDE抱怨

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

您可以在 onClick 方法之外访问和存储该对象,然后在 onClick 内部使用该变量,例如:

val rootComponent = LocalRootComponent.current

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