Working with variables

Variables enable you to manage state throughout your application, allowing you to create dynamic and interactive components.

Creating variables

Create a variable

To create a variable:

  1. 1
    Open the data panel with no element selected
  2. 2
    In the Variables section, click the + button
  3. 3
    Enter a name for your variable
  4. 4
    Set 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
Tip

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 variable
  • true or false creates a boolean variable
  • null is the default and can be used when the actual value will be set later
Info

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.

Tip

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:

  1. 1
    In a workflow or event add a new action by clicking the + button
  2. 2
    Select in Variables the Set: variable action for desired variable
  3. 3
    Specify the new value (static value or via a formula)
Example

See how clicking the card updates the isCardSelected variable.

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:

  1. 1
    Select the element in the element tree
  2. 2
    Find the property you want to bind
  3. 3
    Click the fx button next to the property
  4. 4
    In the formula editor, select your variable from the available options
  5. 5
    Add any additional logic if needed (transformations, conditions, etc.)
Example

See how to bind the isCardSelected variable to a class for the card's conditional styling.

You can bind variables to various aspects of elements:

Input field binding

For input elements, Nordcraft provides a convenient shortcut to create two-way binding with variables:

  1. 1
    Select an input element in the element tree
  2. 2
    In the Attributes tab, find the 'Bind to variable` dropdown
  3. 3
    Choose 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
Example

See how to bind a variable to an input field.

05/02/2025
Edit article
Help Forum