Working with variables
Variables enable you to manage state throughout your application, allowing you to create dynamic and interactive components.
Creating variables
To create a variable:
- 1Open the data panel with no element selected
- 2In the Variables section, click the + button
- 3Enter a name for your variable
- 4Set an initial value (can be a static value or formula)
Initial value
When you create a variable, its default initial value is null
. You can either keep this default value or specify a different initial value:
- Static values: You can directly enter text, numbers, booleans, arrays and JSON objects etc.
- Formula-based values: Click the fx button to open the formula editor to create a dynamic initial value
The initial value serves several purposes:
- Provides a starting state for your component
- Determines the implicit data type of the variable
- Ensures the variable has a known value before any user interaction
The variable type is automatically inferred when you set the initial variable value (e.g. Number
, String
, Array
, Object
or Boolean
).
Initialization examples:
0
creates a numeric variable""
(empty string) creates a string (text) variable[]
creates an array variable{}
creates an object variabletrue
orfalse
creates a boolean variablenull
is the default and can be used when the actual value will be set later
When using a formula to set the initial value, you can reference attributes but not API data. This is because API data won't be available when the variable initializes.
For API-dependent variables, set an initial value like null
and update the variable after the API call completes using the On success event.
Updating variables
You can update variables using workflows or events:
- 1In a workflow or event add a new action by clicking the + button
- 2Select in Variables the
Set: variable
action for desired variable - 3Specify the new value (static value or via a formula)
Binding variables
You can connect variables to element properties through binding to dynamically control the appearance and behavior of your components.
To bind a variable:
- 1Select the element in the element tree
- 2Find the property you want to bind
- 3Click the fx button next to the property
- 4In the formula editor, select your variable from the available options
- 5Add any additional logic if needed (transformations, conditions, etc.)
You can bind variables to various aspects of elements:
- Attribute values (
text
,src
,href
, etc.) - Classes (see conditional styling)
- Show/hide functionality (with boolean conditions)
- Repeat formulas (using arrays to generate multiple elements)
- Workflows and actions (as inputs or conditions to control logic)
Input field binding
For input
elements, Nordcraft provides a convenient shortcut to create two-way binding with variables:
- 1Select an input element in the element tree
- 2In the Attributes tab, find the 'Bind to variable` dropdown
- 3Choose an existing variable or create a new one
This is a shortcut that automatically sets up:
- A binding of the variable to the input's
value
attribute - An event handler on the
input
event that updates the variable when the user types