https://kotlinlang.org logo
#compose
Title
# compose
a

Alan Lee

08/31/2022, 3:44 PM
What is a recommended way to handle a variable that is only used for the very first composition and changed to null so it is not used again. But changing to null causes the recomposition so trying to find a good way to avoid this.
h

hfhbd

08/31/2022, 3:50 PM
Sounds like SideEffect to me. But what is the use case?
a

Alan Lee

08/31/2022, 3:57 PM
It is one of the inputs used to decide what needs to be drawn, but only need to be used the very first time and cleared until it is set with a valid value later.
h

hfhbd

08/31/2022, 4:01 PM
Okay, so it is required to recompose. But why do you want to set it to
null
? just use this variable normally, either as property, mutableState or as key for remember.
a

Alan Lee

08/31/2022, 4:11 PM
so I have it kind of nested. if (a) { render X1 else if (b) { render X2 } So if a is not cleared it will always render X1 but it only should be shown the first time.
Anyway that variable is always checked due to the declarative nature of things and want to set it to null so it is not used again. But also avoid recomposition when setting to null.
m

Mark Murphy

08/31/2022, 5:03 PM
but only need to be used the very first time and cleared until it is set with a valid value later
You have no way of knowing what other recompositions might be needed between "the very first time" and "later". For all you know, the function will be recomposed one frame after "the very first time" and X1 effectively never appears.
a

Alan Lee

08/31/2022, 5:05 PM
Yeah. that is the problem I’m trying to solve. Setting the variable to null triggers a recompose and X1 does not show up. Maybe I need to take a different approach to this.
m

Mark Murphy

08/31/2022, 5:47 PM
You might want to step back and think through the business rules. The business rules aren't going to be expressed in terms of recompositions.
133 Views