What is a recommended way to handle a variable tha...
# compose
a
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
Sounds like SideEffect to me. But what is the use case?
a
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
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
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
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
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
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