State Management & Events
This content is for v1.1.1. Switch to the latest version for up-to-date documentation.
In ZKite, the user interface is a pure function of application state. When state changes, the framework re-evaluates the view and updates native views.
Declaring State with @store
Section titled “Declaring State with @store”Application state is declared using the @store decorator. Annotated fields define the state values with default fallbacks:
from zkite import store
@storeclass ProfileStore: username: str = "Alice" email: str = "" is_notifications_enabled: bool = True tap_count: int = 0
profile = ProfileStore()Annotated store parameters in a view are automatically supplied by the framework when requested.
Mutating State with store.update()
Section titled “Mutating State with store.update()”To change values and request an updated interface, always call the store’s update method:
# Update one or multiple fieldsprofile.update( tap_count=profile.tap_count + 1, is_notifications_enabled=False)Controlled Values
Section titled “Controlled Values”Interactive widgets (TextField, Switch, Checkbox, Radio, Slider, Select) are controlled widgets:
- The displayed value comes from a store field.
- User actions invoke event callbacks that call
store.update(). - If a callback does not update the store, a subsequent render restores the previous value.
from zkite import TextField
TextField( "p-3 bg-slate-900 rounded-lg text-white", value=profile.username, on_change=lambda text: profile.update(username=text), placeholder="Enter username...",)Slow Work and Error Boundaries
Section titled “Slow Work and Error Boundaries”1. Offload Slow Operations
Section titled “1. Offload Slow Operations”Keep callbacks short for responsive 60/120 FPS interaction. Move blocking work (network requests, slow SQLite queries, large file reads) away from the rendering path using Python worker threads:
import threading
def fetch_data(): profile.update(is_loading=True)
def worker(): try: # Perform blocking I/O off the main thread data = do_network_call() profile.update(data=data, is_loading=False) except Exception as err: profile.update(error=str(err), is_loading=False)
threading.Thread(target=worker, daemon=True).start()2. Standalone Error Masking (1.1.1 & 1.1.0)
Section titled “2. Standalone Error Masking (1.1.1 & 1.1.0)”- Standalone APK / AAB: Uncaught Python startup, render, and event exceptions display a generic restart message rather than rendering raw traceback text on user screens.
- Diagnostics: Full tracebacks are preserved in device logcat (
zkite run --logs). - Development Mode:
zkite devcontinues to display full interactive error overlays with detailed code frames. Invalid dependency changes are reported in the dev console without crashing the running app on device.
