Use Reactivity with Signal and Computed
mainTui uses a reactive system to automatically update the UI when data changes.
Signal: A wrapper for a value that notifies listeners when it changes. Use.valueto read or write the data.Computed: A reactive value that derives its state from other signals. When the underlying signals change, theComputedvalue updates automatically.
When accessing a signal's value outside of a reactive context (like inside a standard event callback), use the .peek() method on the component's property to get the current value without creating a dependency.
const number = new Signal(0);
const button = new Button({
parent: tui,
label: {
text: new Computed(() => number.value.toString()),
},
// ...
});
// Updating the signal triggers UI updates
button.state.when("active", () => {
++number.value;
});