Hi,
I’m new to Builder.io and planning to use it to display a few carousels on my website. I’ll be rendering the sections using the React <BuilderComponent> component.
I’d like to know if it’s possible to pass dynamic data, such as image URLs, CTA links, and text, directly from my React code to the Builder component for rendering. If so, could you guide me on how to achieve this?
Looking forward to any advice or examples. Thanks in advance!
Yes, you can pass dynamic data to the <BuilderComponent> in React. This allows you to have dynamic content such as image URLs, CTA links, and text that can be used within your Builder components.
To achieve this, you can use the data property available on the <BuilderComponent>. The data property allows you to pass any React state or props down into your Builder content, which can be bound to elements within Builder using state and actions.
Here is a basic way to pass dynamic data to the <BuilderComponent>:
import { BuilderComponent } from '@builder.io/react';
function App() {
const dynamicData = {
imageUrl: 'https://example.com/image.jpg',
ctaLink: 'https://example.com/cta',
text: 'Welcome to My Carousel!'
};
return (
<div className="App">
<BuilderComponent
model="your-section-model"
data={dynamicData} // Pass your dynamic data here
/>
</div>
);
}
export default App;
In your Builder content, you can use the binding feature to use these data fields. For instance, if you have an image element, you can bind its src attribute to state.imageUrl, a link’s href to state.ctaLink, and text elements can be bound to state.text.
In the Builder Visual Editor, select the element you want to bind the data to.
Go to the “Data” tab.
Bind to the value you passed in, using state.yourValueName. For example, state.imageUrl.
This allows for seamless integration of dynamic content from your React application into your Builder.io components. If you have more questions or need further assistance, feel free to ask!
I haven’t tested it personally, but you can certainly give it a try by passing data props with the builder component. If that doesn’t work, please let us know. We may need to submit a feature request if it doesn’t operate as expected.