Anyone have any opinions on passing a function thr...
# compose
a
Anyone have any opinions on passing a function through a deep hierarchy using CompositionLocals? Is it okay to do that?
t
curious what the use case is for that…a function doesn’t seem like something that should be handled with CompositionLocals…it sounds like trying to do dependency injection of sorts
👍 1
a
Yes, we could probably give better advice with more specifics about the use case. Without additional info I'd recommend no, avoid CompositionLocal and use parameters and/or lexical scoping of lambda capture instead.
👍 1
But that's not specific to what generic type the CompositionLocal might have 🙂
a
Thanks! The use-case is a library that shows a grid with image categories, and tapping on a category shows lots of images in that category. Tapping on the image invokes the function, which is sort of an action handler to close the enclosing (non-Compose) BottomSheet.
a
Definitely avoid a CompositionLocal and just use normal parameters here
🙏 1
a
Thanks! Really appreciate the input! :)
a
MyGrid(data, onImageClick = { imageInfo -> ... }, ...)
Etc.
a
And just keep passing the click further down? It's like 5-10 levels down before the click function is relevant.
a
it depends on the components involved. In practice the structure you described may end up looking like this:
Copy code
MyGrid(dataSource) { item ->
  MyImage(item, onClick = { doStuffHere() })
}
at which point you're not explicitly passing an argument to anything but
MyImage
- none of the other intermediate layers know it exists
👍 1