Widget states
Declare named values for a widget, bind them to components, update them from functions and show or hide parts of the widget based on their value.
Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.
A state is a named value that a widget remembers while it is on screen. Use states for what a customer typed or selected, for data loaded from an action and for simple flags such as "loading". Components read states and functions change them.
Declare states
States are listed under states in the widget definition, each with a starting value.
{
"states": {
"selectedDate": null,
"email": "",
"agreed": false,
"slots": [],
"status": "idle"
}
}
A state can hold text, a number, true or false, a list, an object or null. Each time the agent shows the widget, it starts again from these values.
Read a state
Use a binding in any text or list property.
{ "type": "text", "text": "You picked {{states.selectedDate}}" }
Form inputs write to the state named by their name property, so a text-input with "name": "email" updates states.email as the customer types. Declare the state first, so the input has somewhere to write.
Change a state
Functions change states with a setState step, and callAction steps can store a result with saveTo.
{
"step": "setState",
"key": "status",
"value": "loading"
}
The widget redraws as soon as a state changes. See Functions for the full list of steps.
Show and hide with showIf
Every component accepts showIf, a binding. The component is shown when the value is true, non-empty or not null.
{ "type": "text", "text": "No slots left today.", "showIf": "{{states.noSlots}}" }
A loading pattern
Because showIf checks whether a value is true, use one flag per situation instead of comparing text.
| State | Starts as | Set to true when |
Used to show |
|---|---|---|---|
isLoading |
false |
A function starts a request. | A progress component. |
hasError |
false |
An if step finds that the request returned nothing. |
A short message and a retry button. |
order |
null |
A request succeeds and saveTo stores the result. |
The order details. |
Set isLoading to true before a callAction step and back to false after it. Give the progress component "showIf": "{{states.isLoading}}" and the details card "showIf": "{{states.order}}".
Note: States belong to one shown widget in one conversation. They are not shared between customers or kept after the conversation ends. Store anything you need later by sending it to an action.
Related pages
- Form inputs lists the components that write to states.
- Code editor explains the other bindings you can use.