Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the jetpack compose remember variable from within another function

I am developing an application for android devices, using the jetpack compose library.

Example I created inside a compose function

var taskVeriable = remember {mutableStateOf("Hello World")}

I need to update the value of variable from another compose function. Is there any way to achieve this?

@Composable
fun TestComposeA(){

var taskVeriable = remember {mutableStateOf("Hello World")}

TestComposeB(taskVeriable)

}

@Composable
fun TestComposeB(taskVeriable : String){
taskVeriable = "New Value"
}

I want to know if there is a way to do this.

like image 496
knight Avatar asked Jun 10 '26 19:06

knight


1 Answers

You can pass mutable state and change it value later:

@Composable
fun TestComposeA(){

val taskVeriable = remember {mutableStateOf("Hello World")}

TestComposeB(taskVeriable)

}

@Composable
fun TestComposeB(taskVeriable : MutableState<String>){
taskVeriable.value = "New Value"
}
like image 132
Roman Y Avatar answered Jun 12 '26 11:06

Roman Y



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!