State allows us to create interactive apps that can respond to user input. It defines the variables that can change over time, and the functions that can modify them.
State Basics
The base state is defined as a class that inherits from rx.State.
import reflex as rx
classState(rx.State):"""Define your app state here."""
State is made up of two parts: vars and event handlers.
Vars are variables in your app that can change over time. Event handlers are functions that modify these vars in response to events.
These are the main concepts to understand how state works in Reflex.
Base Var
Any variable in your app that can change over time.
Defined as a field in the State class
Can only be modified by event handlers.
Computed Var
Vars that change automatically based on other vars.
Defined as functions using the @rx.var decorator.
Cannot be set by event handlers, are always recomputed when the state changes.
Event Trigger
A user interaction that triggers an event, such as a button click.
Defined as special component props, such as on_click.
Can be used to trigger event handlers.
Event Handlers
Functions that update the state in response to events.
Defined as methods in the State class.
Can be called by event triggers, or by other event handlers.
Example
Here is a example of how to use state within a Reflex app. Click the text to change its color.
Welcome to Reflex!
from typing import List
classExampleState(rx.State):# A base var for the list of colors to cycle through. colors: List[str]=["black","red","green","blue","purple",]# A base var for the index of the current color. index:int=0defnext_color(self):"""An event handler to go to the next color."""# Event handlers can modify the base vars.# Here we reference the base vars `colors` and `index`. self.index =(self.index +1)%len(self.colors)@rx.vardefcolor(self)->str:"""A computed var that returns the current color."""# Computed vars update automatically when the state changes.return self.colors[self.index]defindex():return rx.heading("Welcome to Reflex!",# Event handlers can be bound to event triggers. on_click=ExampleState.next_color,# State vars can be bound to component props. color=ExampleState.color, _hover={"cursor":"pointer"},)
The base vars are colors and index. They are the only vars in the app that may be directly modified within event handlers.
There is a single computed var, color, that is a function of the base vars. It will be computed automatically whenever the base vars change.
The heading component links its on_click event to the ExampleState.next_color event handler, which increments the color index.
With Reflex, you never have to write an API.
All interactions between the frontend and backend are handled through events.
Client States
Each user who opens your app has a unique ID and their own copy of the state. This means that each user can interact with the app and modify the state independently of other users.
Try opening an app in multiple tabs to see how the state changes independently.
All user state is stored on the server, and all event handlers are executed on the server. Reflex uses websockets to send events to the server, and to send state updates back to the client.