Back to Tutorials
45 min
Intermediate
Creating Interactive Components with Actions
Master the Actions system to build dynamic, interactive components. Learn how to handle clicks, form submissions, data fetching, and state management.
Introduction
Actions are the key to making your Structor projects interactive. In this tutorial, you'll learn how to use Action Flows to add interactivity to your elements, handle user events, manage state, and integrate with APIs.
Step 1: Understanding Actions
Actions in Structor are triggered by events and can perform various operations. The main triggers are:
- On Click: When an element is clicked
- On Hover: When the mouse enters an element
- On Load: When a page or element loads
Step 2: Create App State Variables
- Open Settings (gear icon in the top bar)
- Go to the App State tab
- Click + Add Variable
- Create a variable named
counterwith typeNumberand default value0 - Create another variable named
messagewith typeStringand default value""
Step 3: Create a Simple Counter Button
3.1 Add Elements
- Add a Button element to your canvas
- Set the button text to: "Click Me"
- Add a Text element below the button
- Set the text content to:
{{counter}}(this displays the counter value)
3.2 Add Action Flow
- Select the button
- Open the Actions tab in the right panel
- Click + Add Action
- Select On Click as the trigger
- The Action Flow editor will open
3.3 Add Update State Action
- Click + Add Node in the flow editor
- Select Update State action
- Configure it:
- Variable: Select
counter - Operation: Select
Increment - Value:
1
- Variable: Select
- Close the flow editor
3.4 Test It
- Click Preview to test your counter
- Click the button - the counter should increase by 1 each time
Step 4: Create a Form with Validation
4.1 Add Form Elements
- Add an Input element (for name)
- Add another Input element (for email)
- Add a Button element (Submit button)
- Add a Text element to display messages (below the button)
- Style them as needed
4.2 Configure Input Bindings
- Select the name input
- In the right panel, find Input Binding
- Set it to:
name(this creates a binding to the input value) - Select the email input and set its binding to:
email
4.3 Add Form Submission Action
- Select the submit button
- Open Actions tab
- Add On Click action
- In the flow editor, add a Conditional node:
- Condition:
{{name}} != "" - This checks if name is not empty
- Condition:
- In the "true" branch, add a Show Toast action:
- Message: "Form submitted successfully!"
- Duration: 3000ms
- In the "false" branch, add another Show Toast:
- Message: "Please enter a name"
- Type: Error
Step 5: Make an API Request
- Create a button labeled "Fetch Data"
- Add an On Click action
- In the flow editor, add an HTTP Request node:
- Method: GET
- URL:
https://api.example.com/data - Output Variable:
apiData
- Add a Show Toast after the HTTP request with message: "Data fetched!"
- Add a Text element displaying:
{{apiData}}
Step 6: Navigate Between Pages
- Create a new page in your project (use the Pages panel in the left sidebar)
- Add a button on your first page
- Add an On Click action
- Add a Navigate action:
- Page: Select your new page from the dropdown
- Open in new tab: Leave unchecked
Congratulations!
You've learned how to:
- Create and use App State variables
- Build Action Flows
- Handle user interactions
- Validate forms
- Make API requests
- Navigate between pages
Next Steps
- Explore animations to enhance interactions
- Learn about data integration with Supabase
- Check the Actions documentation for advanced features