State
Last updated
Last updated
© 2024 UI Bakery
State variables function as temporary storage while users interact with your app. They reset to their initial values upon a page refresh or when the app is closed. State variables persist solely within a single page.
For persistent storage that remains intact upon page reloads, consider exploring the Local Storage documentation.
State variables come in two scopes: app and page. App scope variables maintain their values across different pages, while page scope variables retain their values only within the specific page.
A state variable can possess the following attributes:
Name - the unique identifier;
Type - string|number|boolean|object|arrray;
Initial value - the starting value of the variable.
State variables can be organized using the folder structure of the Actions panel, allowing you to place relevant variables near the actions and logic they are associated with.
To create a state
variable, follow these steps:
On the Actions panel, click the + button to add a new item and select State Variable in the dropdown menu.
Give your variable a name, and assign its initial value.
Click Save.
Depending on the scope in which the variable is created, it is now available either in the whole app or on a particular page.
Now this variable is available in your UI Bakery app. You can read and write to this variable.
Upon creating a state variable, it becomes accessible within the app.
You can assign a value to the variable using the "Save to State" action or directly through JavaScript code blocks.
Within the app, all variables can be accessed using the {{state.<variableName>}}
scope:
To store a value within an action, follow these steps using the Save to State action step:
Choose Save to State.
Select the variable you wish to modify.
Define a new value, which can either be hardcoded or derived from an available variable, such as {{data}}
to reference the result of a previous action step, or {{ui.component.value}}
to access a component's state.
Additionally, you can save state values in any JavaScript code block:
read with the state.getValue
method:
reset a variable to the initial value:
or reset all variables:
Let's check how to use a state variable on an example. Say you have a text component and would like to reset its' value after a comment is left. Here's how to do that:
Create a state variable of a string type.
Assign the state variable to the text Value field:
3. Create a Save to State action, that will save the new comment and assign it to the On Change trigger of the text component:
4. Next, add another Save to State action, that will reset the comment:
5. Finally, drag a button onto the working area and assign the reset action on its On Click trigger.