Learn how to ship impactful customer journeys with Builder

Announcing Visual Copilot - Figma to production in half the time

Builder.io logo
Contact Sales
Platform
Developers
Contact Sales

Blog

Home

Resources

Blog

Forum

Github

Login

Signup

×

Visual CMS

Drag-and-drop visual editor and headless CMS for any tech stack

Theme Studio for Shopify

Build and optimize your Shopify-hosted storefront, no coding required

Resources

Blog

Get StartedLogin

Custom actions give you a powerful tool for creating dynamic and interactive experiences in Builder. You can add new functionality to your UI, reuse code across multiple components, and create a more engaging and user-friendly experience for your users.

Tip: The techniques covered in this document are for the Gen 1 React SDK. For detailed information on the Gen 2 SDKs, visit SDK Comparison.

To pass data down, you can use the data prop in the BuilderComponent and assign it an object with key-value pairs. For example, you can pass a list of products and additional data, such as an isLoggedIn boolean:

<BuilderComponent
  model="page"
  data={{
    products: productsList,
    isLoggedIn: true,
  }}
  content={builderJson}
/>

The data passed down is available in Builder actions and bindings using the prefix state.*. For example, state.products refers to the productsList passed down in the example above.

You can also pass down functions and complex data using the context prop. For example:

<BuilderComponent
  model="page"
  context={{
    addToCart: () => myService.addToCart(currentProduct),
    lodash: lodash,
  }}
  content={builderJson}
/>

Here, the context object is assigned two key-value pairs:

  • a function addToCart()
  • the library lodash

The context passed down is available in Builder using the prefix context.*. For example, context.lodash refers to the lodash library passed down in the example above.

You can add an action to any element, though button actions are frequently customized, which this section covers.

The following example demonstrates a context object that defines a single function called myFunction(), which displays an alert with "Hi!" when called.

export default () => (
  <BuilderComponent 
    name="page" 
    context={{ 
      myFunction: () => alert('Hi!') 
    }} 
  />
)

By passing down functions using the context prop, you can create flexible and dynamic UI components in the Builder Visual Editor that respond to user input and other events.

To assign the function to run on click of a button:

  1. Select the button.
  2. Go to the Data tab.
  3. Expand the Element events section. For this example, leave the default of On to click.
  4. Click the + New Event button.
  5. Click Edit Action > + Action > Custom Code.
  6. Add your custom Javascript. In this example, add context.myFunction().

The following video demonstrates this process:

After you've set up a custom action on an element, such as a button, you can save the element as a Template or Symbol for reusability.

In addition to custom actions and passing data down with BuilderComponent, Builder supports data binding directly within rich text fields. This feature means you can dynamically insert content into any text or link value using state variables.

When using data binding in rich text fields, for example, state.someValue, you must set up your Node server correctly so that this functionality works in production environments.

For more information on proper setup, including the use of the isolated-vm package, read the Enabling data bindings in Node environments in Integration Tips.

Use double curly braces to insert state variables:

{{state.someValue}}

Text Content

You can use data binding for any text content. For example, to create a personalized greeting:

Hello {{state.userName}}!

Link URLs

Data binding also works with hyperlink URLs:

Hello {{state.userName}}!

This feature works with any text content, both in inline text fields and Rich Text Editor (RTE) fields. No additional configuration is required within the Builder interface to use the undefined syntax.

However, to populate these state values with actual data, you need to pass the data to the BuilderComponent in your code, similar to how you pass data for custom actions:

The data passed this way is accessible using state.* in the rich text fields, just as it is in Builder actions and bindings.


  • Better support for localization (coming soon): The content API will provide improved support for localization, including querying based on localization features.
  • Support for multi-level nested references (coming soon): The content API will allow you to query, resolve, and return content that has nested references of other contents and symbols.
Was this article helpful?

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Newsletter

Get the latest from Builder.io

By submitting, you agree to our Privacy Policy

Product

Platform Overview

Integrations

What's New

Open Source

Builder

Builder

Mitosis

Mitosis

Qwik

Qwik

Partytown

Partytown

Popular Guides

From Design to Code Guide

Composable Commerce Guide

Headless CMS Guide

Headless Commerce Guide

Composable DXP Guide

Design to Code

Resources

Blog

Knowledge Base

Community Forum

Partners

Templates

Success Stories

Showcase

Resource Center

Frameworks

React

React

Next

Next.js

Qwik

Qwik

Gatsby

Gatsby

Angular

Angular

Vue

Vue

Svelte

Svelte

Remix logo

Remix

Nuxt

Nuxt

Hydrogen

Hydrogen

See All

© 2024 Builder.io, Inc.

Security

Privacy Policy

SaaS Terms

Security & Compliance

Cookie Preferences

Gartner Cool Vendor 2024