I have a question on state hoisting. With testing ...
# compose
t
I have a question on state hoisting. With testing code:
Copy code
@Composable
fun F() {
    val state = remember { mutableStateOf("Test") }
    A(state)
    B(state.value)
    /* ... */
}

@Composable
fun A(state: State<String>) {
    Text(text = state.value)
}

@Composable
fun B(value: String) {
    Text(text = value)
}
Can we claim that
A
is always better than
B
, because when `state`'s value changes
B(state.value)
causes
F
to recompose, while
A(state)
does not cause
F
to recompose? Or does it even matter in practice?
👍 1
🚫 2
s
If you do end up wanting to defer the read, do
fun A(getState: () -> String)
instead of passing the
State
around. Other than that, if you always do it it's just gonna be clunky in the API and in many cases you won't even get any performance benefit from it. So I'd say do it only when you know you need it, not always.
m
I think it can be said that assuming A and B need this value in a nested composable, then you can assume that A will always be doing less composition than B, so it will be more efficient.
Using function type
() -> String
would secure and generalize it, so I agree it would be beneficial.
s