Reactlit works by defining an application script. This script procedurally builds the UI, inserting elements
into the DOM as needed. Similar to how Streamlit works, every state change reruns the script which
in turn re-renders the UI. Reactlit employs a few tricks to try to minimize re-renders so that you should not
see any flickering or lose focus state.
Primitives
Reactlit core provides the following set of primitives to use in your application script.
Display
The display function is used to render a React node.
It can optionally take an first argument string to be an id for the display.
This allows later code to update the display.
Set State
The set function is used to set state. Most of the time you will not need to
use it directly, instead you will use the view function below, but it is necessary on occasion.
Additionally the raw state object is also provided as part of the context.
View
The view function is used to render an input view which has state and to return that state.
Think of it as a special form of display with a return value.
The first argument is the key of the view. This determines which state key the value will be stored in.
The second argument is the view definition. Some built-in view definitions are provided in the add-on packages like
@reactlit/vanilla and @reactlit/radix. Or you can define your own as described in the Defining Views guide.
The follow examples assume you have imported the Inputs object from the @reactlit/radix package.
Views are type-safe and the types are determined by the view definition itself.
Transform views
Some views define a getReturnValue function which transforms the value returned by the view.
Transforms allow the state you want to store (what you would get back from the get function) to be different
from the value that is returned to work with. This is especially useful for elements like tables where
the selected state should be some kind of a row id, but the view should return the full row data.
Assuming you have a variable users which is an array of user objects, you can define a view like this:
Changed
Sometimes you need to trigger side-effects when state changes. In React, we have useEffect to handle this.
The equivalent in Reactlit is a changed function which tells you if the provided state keys have changed since the
last run of the script.
We use the changed function here to reset the state of the email input when the selected user changes.
Trigger
Sometimes you need to trigger the script to rerun even if state has not changed. Typically you do this
when you apply mutations and want to refetch data.
Putting it all together
The following example application puts together all of these primitives so you can see how they fit together.