Skip to content

State and Model View Update

David Ortinau edited this page Aug 27, 2019 · 1 revision

State in Comet is managed with the Model-View-Update pattern. A model state object is the source of truth. As anything updates the state of your view, Comet reacts by re-evaluating the View and Updating only what has changed.

This differs from traditional MVVM with data binding in that data only flows 1 direction, from the state object to the view.

Let’s look at a simple example right from our template code:

public class CounterView : View
{
	readonly State<int> count = 0;

	[Body]
	View body() => new VStack(){
		new Text(()=>$”You have tapped {count.value} times.”),
		new Button(“Increment”, ()=>count.value++)
	};	
}	

The change to state in the Button action triggers the Body to re-evaluate. Because the Comet UI is not on the native UI thread, this is fast!

Clone this wiki locally