Home > other >  How to return value in Jetpack Compose
How to return value in Jetpack Compose

Time:11-06

I have a Composable with a Boxand some text and it also returns a value
How to use that value

@Composable
fun dummyAndUselessUI(String:String) : String{
    val text = remember { mutableStateOf("") }
    Box(modifier = Modifier.size(100.dp)){ Text(String) }
    return text.value
}

CodePudding user response:

You don't need a function that return a value, in Compose you handle State

@Composable
fun dummyScreen() {
    var text by rememberSaveable { mutableStateOf("") }

    dummyAndUselessUI(text = text, onNameChange = { text = it })
}
@Composable
fun dummyAndUselessUI(text: String, onTextChange: (String) -> Unit) {
    Box(modifier = Modifier.size(100.dp)){ 
       OutlinedTextField(
          value = text,
          onValueChange = onTextChange,
          label = { Text("Name") }
       )
    }
}
  • Related