简体   繁体   English

变量不使用jetpack compose在可组合内部更新

[英]Variable doesn't update inside composable using jetpack compose

I'm trying out jetpack compose and have a basic textfield composable that accepts a boolean variable for validation.我正在尝试jetpack compose,并有一个基本的可组合文本字段,它接受一个布尔变量进行验证。 However it doesn't update properly and only works at initialisation.但是它不能正确更新,只能在初始化时工作。

class RegistrationActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {

        var name = "zzz"

        BasicField(
            title = "Last Name",
            value = name,
            onValueChange = {name = it},
            placeholder = "Enter Last Name",
            validation = (name.length>2&&name.contains("zzz"))
        )
    }
}}

composable:可组合:

@Composable
fun BasicField(title: String,
           value: String,
           onValueChange: (String) -> Unit,
           placeholder: String,
            maxLines: Int = 1,
            validation: Boolean ) {

var fieldValue by remember { mutableStateOf(TextFieldValue(value)) }

BasicTextField(
    maxLines = maxLines,
    value = fieldValue,
    onValueChange = {
        fieldValue = it

        if(fieldValue.text.length>5){//this part works
            Log.e("error","valid string")
        }else{
            Log.e("error","invalid string")
        }

        if(validation){//this part doesnt work
            Log.e("error","custom validation works")
        }else{
            Log.e("error","custom validation failed")
        }


    },


) }

I have a basic validation inside the composable which checks for string length which works, but when the logic is from outside it doesn't work.我在可组合物中有一个基本验证,它检查字符串长度是否有效,但是当逻辑来自外部时它不起作用。 I appreciate any help or hint thanks!我感谢任何帮助或提示谢谢!

You need to make your name as state in compose.您需要在撰写中将您的名字作为状态。 Here we have used 2 things.在这里,我们使用了 2 个东西。

  1. name is defined as state. name被定义为状态。 And it is remembered in composition.它在作文中被记住。 So whenever this composable function goes into re-composition, this name will not be re-assigned.因此,每当这个可组合函数进入重新组合时,这个名称将不会被重新分配。

  2. Used LaunchedEffect to execute your logs statements.使用LaunchedEffect执行您的日志语句。 So LaunchedEffect will start with a key and when they key changes, this effect will restart.所以 LaunchedEffect 将从一个键开始,当它们键更改时,此效果将重新启动。


class RegistrationActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {

                // 1
        val name = remember { mutableStateOf("") }

        BasicField(
            title = "Last Name",
            value = name.value,
            onValueChange = {name.value = it},
            placeholder = "Enter Last Name",
            validation = (name.value.length>2&&name.value.contains("zzz"))
        )
    }
}}


@Composable
fun BasicField(title: String,
           value: String,
           onValueChange: (String) -> Unit,
           placeholder: String,
            maxLines: Int = 1,
            validation: Boolean ) {

    // 2
    LaunchedEffect(key = value) {
        if(value.length>5){
            Log.e("error","valid string")
        }else{
            Log.e("error","invalid string")
        }
        
        if(validation){
            Log.e("error","custom validation works")
        }else{
            Log.e("error","custom validation failed")
        }
    }
    
    BasicTextField(
        maxLines = maxLines,
        value = value,
        onValueChange = {
            onValueChange(it)
            },
    ) 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM