So i’m toying with putting a LoggingContext in a c...
# compose
m
So i’m toying with putting a LoggingContext in a composition local, and then using that inside my onClick events to do click logging. There’s two things here: 1. I have to get the loggingContext outside of the onClick, since it requires being in a composable environment, so i save it to a variable first and can reference it in onClick 2. I’m triggering a side effect by doing logging. I suppose this is kind of expected when clicking buttons. I’m just curious what the general consensus is on triggering side effects in an onClick (i can’t use SideEffect itself, but the function i’m calling is side effecting because it’s logging stuff)
Copy code
val loggingContext = LocalLoggingContext.current

    Button(
        onClick = { 
            loggingContext.log(...)
            ...
        }
    ) { ... }
The other option i had was passing down a lambda function and calling that in the onClick callback. This just makes it so i don’t have to pass the lambda function all the way down the tree.
c
The
onClick
lambda is not called inside the composition, so it is ok to have side effects in it.
☝🏻 1
m
thanks @Csaba Kozák. That's pretty much what i was assuming.