Skip to main content

State Variables

State Variables allow users to create Temporary (for the user session, resets on refresh) and Local Storage (stored in the browser across sessions) variables for their Superblocks Applications. The Local Storage variant makes use of the browsers Storage interface to write data to and read data from the client.

Variables can be used by builders to store local state in their applications for use-cases like: caching and re-use of intermediate state across different APIs/Components.

The context menu for state variables is located in state variable panel on the left-hand side of the App Builder.

State variables allow users to create temporary and persistent variables for their Applications

Create a variable

Create a variable from the state variable panel

To create a variable from the state variable panel open the VAR section from the state variable panel and click on the Add button. Choose whether to create a Temporary state variable or a Local Storage state variable from the dropdown menu.

Create a new State Variable from the context menu in the state variable panel

By default the value of a new variable will be set to null:

The default value of a newly created State Variable is Null

To change the default, set the variables Default value in the properties panel on the right-hand side of the app builder:

The user can set a custom default value for a State Variable in the Properties Panel

The values passed to a state variable will be parsed as JSON. A variable can hold any type of data, including an object with multiple key-value pairs:

A State Variable can hold any type of date including objects with multiple key-value pairs

The value a state variable has will be displayed in the context menu. If a state variable holds more than one key-value pair then the list will have an entry for each pair:

The variable in the state variable panel show the custom default value set by the user

Create a variable from the Properties Panel

State variables can also be created from the event handler menu in the applications properties panel.

Select Set State Variable from the list of available action types. Click on State Variable to open the dropdown menu and create a new Temporary State Variable or Local Storage State Variable:

State Variables can be created from the event handlers menu in the Properties Panel

Delete a variable

To delete a state variable, select the variable from the state variable panel, click on the three-dots menu and click on Delete:

State Variables can be deleted from the state variable panel

Set a variable

info

State variables are stored on the client. Therefore, they live in the frontend scope of an application and cannot be set from a backend API.

Set a variable by an event handler

Set state variables in response to events using the Set State Variable action type that can be triggered by event handlers.

Set a variable from code in Run JS (Frontend)

Set state variables from code using the predefined variable.set() and variable.setProperty() functions that can be executed from Run JS action types in the frontend of your Superblocks Application. There are important considerations when using frontend variables to avoid retrieving stale values.

Reset a variable

Reset a variable from the state variable panel

To reset a state variable to its default value, select the variable from the state variable panel and click on the Reset to default icon. If the user has not defined a custom default value, value is reset to null. A state variable can also be reset using an event handler.

State Variables can be reset to default from the state variable panel

Reset a variable by an event handler

Reset State Variables in response to events using the Reset State Variable to Default action type that can be triggered by event handlers.

Reset a variable from code in Run JS (Frontend)

Reset state variables from code using the predefined resetToDefault() function that can be executed from Run JS action types in the frontend of your Superblocks Application.

Reference a variable

Reference variables in the frontend

Using the binding syntax, the data stored in a state variable can be referenced by any frontend component in your Superblocks Application:

State Variables can be referenced by frontend components using the binding syntax

State Variables can also be referenced in Run JS action types which is JavaScript that runs in the frontend. In this case the binding syntax is not needed:

State Variables can be referenced in frontend Run JS action types

Reference variables from the backend

In the example below the value of the name property that is stored in the state variable TempVar1 is returned from a JavaScript step in a backend API:

State Variables can be referenced in backend APIs of Supeblocks applications