Hi, I have the following code ```@Composable fun M...
# compose
e
Hi, I have the following code
Copy code
@Composable
fun MyComposableFun(textValue: String) {

    val newTextValue = remember(textValue) { mutableStateOf(textvalue) }

    val myComposeView = remember {
        MyComposable().apply {
            setContent { Text(newTextValue) }
        }
    }

    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        Text("Title: $newTextValue")
        myComposeView.myComposable?.invoke()
    }
}

class MyComposable {
    var myComposable: (@Composable () -> Unit)? = null

    fun setContent(content: @Composable () -> Unit) {
        myComposable = content
    }
}
On initial load it's okay. But when
textValue
changed, and the function got called again, • The
Text("Title: $newTextValue")
got updated - which is good • The
setContent { Text(newTextValue) }
don't get updated. Why? Did I get anything wrong?
s
You’re inside a
remember
there and you’re not passing any keys to it, which means that it will run once and never again. Although I am curious to see how the insides of
MyComposable
looks like, it looks quite odd from looking at it like this.
e
Thanks. If we pass the
key
to it, it will reconstruct the entire
MyComposable
. I try to avoid
MyComposable
to be reconstructed.
I can solve it using
Copy code
@Composable
fun MyComposableFun(textValue: String) {

    val newComposeView = @Composable { Text(textValue) }
    val myComposeView = remember {
        MyComposable().apply {
            setContent (newComposeView)
        }
    }

    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        Text("Title: $textValue")
        myComposeView.myComposable?.invoke()
    }
}

class MyComposable {
    var myComposable: (@Composable () -> Unit)? = null

    fun setContent(content: @Composable () -> Unit) {
        myComposable = content
    }
}
But not this
Copy code
@Composable
fun MyComposableFun(textValue: String) {

    fun newComposeView() = @Composable { Text(textValue) }
    val myComposeView = remember {
        MyComposable().apply {
            setContent (newComposeView())
        }
    }

    Column(horizontalAlignment =     Alignment.CenterHorizontally) {
        Text("Title: $textValue")
        myComposeView.myComposable?.invoke()
    }
}

class MyComposable {
    var myComposable: (@Composable () -> Unit)? = null

    fun setContent(content: @Composable () -> Unit) {
        myComposable = content
    }
}
s
Thanks. If we pass the key to it, it will reconstruct the entire MyComposable. I try to avoid MyComposable to be reconstructed.
Yes, I’m just saying why I think it doesn’t update. This structure in general is a bit odd with creating a class which then contains the function instead of having the function itself, but can you try (in the original snippet) doing smth like
val updatedNewTextValue = rememberUpdatedState(newTextValue)
and pass that inside your composable instead of passing
newTextValue
directly and see if this changes anything?