How to save the dynamically added traits
I made it work by having a count attribute on the component and then adding the trait count times in the init of the container. This is the correct way. Traits are intentionally skipped from the JSON, otherwise, you'd put on hold your component definition in the storage (eg. the day you'll upgrade the component with n...
Read full answer below ↓Question
Hi, @artf, I have a custom component that has a button trait. When the button is pressed, I am adding a custom trait dynamically using comp.addTrait().
When I load the saved template, I want the container to show the dynamically added traits too. (I am loading the template using components but the dynamically added traits are not being shown). What might I be doing wrong?
Answers (4)
I made it work by having a count attribute on the component and then adding the trait count times in the init of the container. This is the correct way. Traits are intentionally skipped from the JSON, otherwise, you'd put on hold your component definition in the storage (eg. the day you'll upgrade...
@artf, thanks for replying. That was a really valuable piece of information.
I made it work by having a count attribute on the component and then adding the trait count times in the init of the container.
Thanks for reporting this, @theSC0RP. Great question about How to save the dynamically added traits. The recommended approach with Components is to use the event-driven API. Start here: Check the GrapesJS documentation for your specific module Look for the on() event listener method Most operations...
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3329
How should I receive custom trait modifications in the script?
Hi, @artf, this is a great framework, thank you very much. I am now using it to create components of the mobile BUI framework, and I ran in...
Issue #3119
How to create custom block made of grapesjs components?
Hi, i want to have custom blocks made of grapesjs components. I found solution how to add components: https://github.com/artf/grapesjs/issu...
Issue #6152
CSS added via custom code persists after custom code component is removed
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? any Reproducible demo link https://grapesj...
Issue #3763
V0.17.25 . Trait button in the custom component defining.
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? FirefoxReproducible demo link https://jsfidd...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Related tutorials
In-depth guides on the same topic.
Tutorial
Introducing GJS.Market Services: ship your GrapesJS editor without the integration risk
It's everything that usually eats weeks of engineering time, scoped and priced up front. No open-ended hourly surprises.
Tutorial
GrapesJS custom components guide (2026)
Define custom GrapesJS components with DomComponents.addType: detection with isComponent, a model with traits and defaults, and a view for canvas behavior.
Tutorial
Building an AI-Assisted Document Engine with Next.js, GrapesJS, and the AI SDK
Transform your static template editor into an intelligent, generative design workspace with Vercel AI SDK, AI Elements and Next.js.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.