Components are the building blocks for your app's user interface (UI). They are the visual elements that make up your app, like buttons, text, and images. Reflex has a wide selection of built-in components to get you started quickly.
Components are created using functions that return a component object.
Click Me
Components can be nested inside each other to create complex UIs.
To nest components as children, pass them as positional arguments to the parent component. In the example below, the rx.text and my_button components are children of the rx.box component.
This is a page
Click Me
You can also use any base HTML element through the rx.el namespace. This allows you to use standard HTML elements directly in your Reflex app when you need more control or when a specific component isn't available in the Reflex component library.
Apps need to store and display data that changes over time. Reflex handles this through State, which is a Python class that stores variables that can change when the app is running, as well as the functions that can change those variables.
To define a state class, subclass rx.State and define fields that store the state of your app. The state variables (vars) should have a type annotation, and can be initialized with a default value.
So far, we've defined state vars but we haven't shown how to change them. All state changes are handled through functions in the state class, called event handlers.
Event handlers are the ONLY way to change state in Reflex.
Components have special props, such as on_click, called event triggers that can be used to make components interactive. Event triggers connect components to event handlers, which update the state.
0
Increment
When an event trigger is activated, the event handler is called, which updates the state. The UI is automatically re-rendered to reflect the new state.
Event handlers can also take in arguments. For example, the increment event handler can take an argument to increment the count by a specific amount.
0
Increment by 1Increment by 5
The on_click event trigger doesn't pass any arguments here, but some event triggers do. For example, the on_blur event trigger passes the text of an input as an argument to the event handler.
Make sure that the event handler has the same number of arguments as the event trigger, or an error will be raised.
Before we dive deeper into state, it's important to understand the difference between compile-time and runtime in Reflex.
When you run your app, the frontend gets compiled to Javascript code that runs in the browser (compile-time). The backend stays in Python and runs on the server during the lifetime of the app (runtime).
We cannot compile arbitrary Python code, only the components that you define. What this means importantly is that you cannot use arbitrary Python operations and functions on state vars in components.
However, since any event handlers in your state are on the backend, you can use any Python code or library within your state.
As mentioned above, you cannot use Python if/else statements with state vars in components. Instead, use the rx.cond function to conditionally render components.
Reflex apps are created by instantiating the rx.App class. Pages are linked to specific URL routes, and are created by defining a function that returns a component.