Defining Views
Though we provide some pre-built views in the @reactlit/vanilla
and @reactlit/radix
packages,
you can also define your own views. This lets you reuse components you already have in your application.
Basic Views
To define a view, you can use the defineView
function. It takes a functional component that has the following props:
stateKey
: The key for the view in the state object.value
: The current value of the view.setValue
: A function to set the value of the view.
Here is an example of how you can define a basic view:
Often, you will want to pass props that can customize your view. For this, you can follow a pattern
where you return the view from a function with args. For example, you can pass a label to the TextInput
view:
You can then use this view in a Reactlit script like so:
Transform Views
You can use the defineTransformView
function to define a view that returns a different value than the one
you pass in. Transform views are explained in the transform views section.
This function is similar to defineView
, but it takes an additional argument, a function that takes the view props
and returns the value you ultimately want to return from the view to the script.
Here is an example view that takes a list and filters it.
Now you can use this view in a Reactlit script like so: