简体   繁体   中英

A change on the state does not recompose the view

I have the following issue where the state change on the counter variable does not trigger recomposition of the Text component. Any ideas of what might be the problem???

@Model
class CounterState(var counter:Int = 0)

@Composable
fun CounterView(counterState: CounterState){
     Column {
         Text(text= "${counterState.counter}", 
              style=(+typography()).body1)

         Button(text ="Increment", 
                onClick = {counterState.counter++},
                style = ContainedButtonStyle())
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        app {
            CounterView(CounterState())
        }
    }
}

var counter by +state { 0 } Column { Text( text = "$counter", style = (+typography()).body1 ) Button( text = "Increment", onClick = { counter++ }, style = ContainedButtonStyle() ) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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