Radoslaw Juszczyk
06/15/2023, 9:11 AMCompositionLocal
that holds the ViewModel
of a particular screen so that all composables in that screen can get a reference to the ViewModel
to perform some logic. This is a bad practice because not all composables below a particular UI tree need to know about a ViewModel
.
and am wondering how to interpret it.
If you take lets say Coils LocalImageLoader
it will turn out to be a bad practice too as not all composables below a particular UI tree need to know about the ImageLoader. Then if you think about it there would be no good useage of a CompositionLocal
Then for the part :
Avoid CompositionLocal
for concepts that aren't thought as tree-scoped or sub-hierarchy scoped.
Lets have a comopsable XyzScreen
which utilizes XyzViewModel
and defines local composition of XyzViewModel
which is used inside XyzScreen
composables - isnt it a tree-scoped or sub-hierarchy scoped concept and therefore a good candidate to use CompositionLocal?Stylianos Gakis
06/15/2023, 9:21 AMRadoslaw Juszczyk
06/15/2023, 9:32 AMStylianos Gakis
06/15/2023, 9:34 AMRadoslaw Juszczyk
06/15/2023, 9:35 AMStylianos Gakis
06/15/2023, 9:40 AMRadoslaw Juszczyk
06/15/2023, 9:54 AMImageLoader
explicitly too, but the problem is that if you do not pass it the AsyncImage
wont complain and will simply resort to use LocalImageLoader.current
. So just to avoid wrong ImageLoader being used we provide it on the most top level. For the other use cases I agree about discouraging it unless it is really frustrating...Stylianos Gakis
06/15/2023, 10:00 AMRadoslaw Juszczyk
06/15/2023, 10:07 AMStylianos Gakis
06/15/2023, 10:10 AMdewildte
06/15/2023, 5:43 PMStylianos Gakis
06/15/2023, 7:12 PM