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

ursus

02/29/2024, 11:53 PM
Copy code
@Composable
fun ReviewTextField( <-------------------------------
    fieldName: String,
    value: String,
    onValueChange: (String) -> Unit,
) {
    Column(
        modifier = Modifier.fillMaxWidth(),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        ...

        OutlinedTextField( <-------------------------
            modifier = Modifier
                .fillMaxWidth(),
            value = value,
            placeholder = { Text(text = fieldName, fontWeight = FontWeight.Normal) },
            onValueChange = onValueChange,
            textStyle = MaterialTheme.typography.body1Strong,
            singleLine = true,
            ...
        )
    }
}
Is it normal for
ReviewTextField
to get recomposed everytime, even if the input don't change if it contains a
OutlinedTextField
? If I turn the text field into a
Text
then the parent doesn't recompose As if
TextField
was causing it to be unstable Wtf? // If I remove the
onValueChange
lambda, then it stops recomposing even with
OutlinedTextField
there .. is lambda making the whole thing non skippable?
a

agrosner

03/01/2024, 12:50 AM
Strong skipping probably tries to fix this
u

ursus

03/01/2024, 12:51 AM
so am I not imagining thing? btw why would a lambda be unstable?
u

ursus

03/01/2024, 12:51 AM
is it because of the anonymous class legacy crap? i.e. different instances etc?
a

agrosner

03/01/2024, 12:51 AM
“All lambdas in composable functions are automatically remembered. This means you will no longer have to wrap lambdas in remember to ensure a composable that uses a lambda, skips.”
So probably to fix above is to wrap the outer most lambda in a remember block
u

ursus

03/01/2024, 12:52 AM
works .. great!
a

agrosner

03/01/2024, 12:52 AM
“the Compose compiler will only wrap lambdas in composable functions that only capture stable values in a remember function”
u

ursus

03/01/2024, 12:52 AM
but is a bit perplexing would a lambda of primitive types be unstable I get that for foreign custom types you cant tell
a

agrosner

03/01/2024, 12:53 AM
The outer most lambda - you might be capturing an unstable object?
Where review text field is called from
u

ursus

03/01/2024, 12:54 AM
well its telescoped all the way to the screen, and there its
onValueChanges = viewModel::valueChanged
and yea view model is obviously unstable
so thats fucking why omg ! thanks!
a

agrosner

03/01/2024, 12:54 AM
No problem! lol
u

ursus

03/01/2024, 12:55 AM
never occured to me to @Stable viewmodels
oh neat in strong skipping mode you dont have to @Immutable/Stable things at all
a

agrosner

03/01/2024, 1:23 AM
Yeah it’s a fundamental change but a great one. The react compiler is doing a similar thing. https://blog.stackademic.com/react-19-how-react-compiler-will-take-your-code-to-the-next-level-8a89702d8860
3 Views