Is there a event per component before save which the component can listen to?
I'm not sure if such hooks exist per component but you can try to take advantage to the toHtml property of a component. It's already been discussed elsewhere so you search it in the issues as there are quite a few.
Read full answer below ↓Question
Much grateful to you for this library.
Background: I am building an editor where I intend to define most of the components from an existing set of Vuejs components which I have developed earlier and use in my applications. Most of these are UI based simple and complex components. For example, I have simple vuejs components named su-form, su-field, su-label, su-input, su-radios, su-buttons, su-validations and complex vuejs components using the simple components like su-login-form, su-registration-form etc. As you may guess, su-form or su-field and other such components has slots where I add other components to create a useful component with a business use case. For example, I insert su-input, su-label, su-button inside su-form's slots to create a proper useful form. Each of these simple components when rendered has a number of HTML DOM elements to take care of various needs. For example, su-input is not just one <input> element, but it also has elements to show respective label, validation error(s) and hint.
My intent is to create grapejs components and block from these simple vuejs components, which I am able achieve to some extent so far. My grapesjs component models' tagNames are vuejs component names (like "su-form"). But I am using a custom processor to render and mount the vuejs components and use the element's ($el) outerHTML to define the model's component. The plan is to make some of these rendered elements styleable. I am loosing all reactivity, but will take this part later.
Now, where I am majorly stuck is when I am saving/storing, it's saving the rendered HTML of the whole editor, which is obvious. What I want is this: if my component can listen to an event which will kind of say this component will be saved/stored (kind of before-save/store) the event parameter provide me what is going to be saved for this component only (HTML, CSS, JSON) and allow me to tweak these, I could replace the HTML and JSON etc. to the original vuejs components (instead of the rendered HTML). My actual page (not editor) has a parent vuejs component which can render these vue-compoents. In that way I can have best of the both worlds.
In another way, the solution can be to have two sets of models for one component, one for the editor and another for the actual page. But I am not sure, if that's possible here.
Please let me know if there's a way which I am missing.
Also, please let me know if this is the right place to discuss this and similar aspects. I actually have a few more queries for which I search for but could not yet find answers.
Thanks in advance.
Answers (3)
I'm not sure if such hooks exist per component but you can try to take advantage to the toHtml property of a component. It's already been discussed elsewhere so you search it in the issues as there are quite a few.
Yeap, you can define your components with a custom toHTML/toJSON function (in model), in order to customize their output
Thanks for reporting this, @sudiptochoudhury.
The issue with Is there a event per component before save which the component can listen to? appears to be a race condition or state management timing problem. This typically happens when component lifecycle events and DOM modifications overlap, creating an inconsistent state.
What to try:
- Add a setTimeout wrapper to ensure the DOM has settled:
setTimeout(() => {
// your operation here
}, 0);
-
Check initialization order — make sure components are fully loaded before you interact with them
-
Use the editor's event system — listen to completion events:
editor.on('component:mount', (component) => {
// safe to interact with component here
});
Recommended next steps:
- Test with the latest GrapesJS version if you haven't
- Provide a minimal reproducible example (CodeSandbox) — this helps the team identify the root cause faster
- Include GrapesJS version, browser, and console errors in your report
Related Questions and Answers
Continue research with similar issue discussions.
Issue #5718
Possible upgrade path issue where 'textnode' is now called 'wrapper' from 0.18 -> latest
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Version 122.0.6261.95 (Official Bui...
Issue #4904
getHtml() producing invalid structure when there is js inside it
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? chrome latestReproducible demo link https://...
Issue #4225
BUG : The facebook plugin is not working in the grapes js
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome v9Reproducible demo link Not thereDes...
Issue #4704
Angular Component as Custom Color Picker is only created once
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Version 104.0.5112.101 Reproducible demo l...
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
Find the Right GrapesJS Plugin in Seconds: Smarter Discovery Is Live
We're shipping a set of discovery upgrades. New label filters, a proper compatibility switch for GrapesJS vs Studio, one-click and a smarter sort bar.
Tutorial
GrapesJS vs Webflow vs Tilda: What to Choose for Your Business in 2026
Choosing the right website platform in 2026 is no longer just about building a site
Tutorial
GJS Market 2.0 - Donations, Tracking, Labels and Better Product Discovery
We’ve rolled out a new set of GrapesJS marketplace updates across GJS Market, focused on improving how creators distribute products
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.