Ids gets changed while trying to set components on the canvas using json
Hi @shelendravashishtha2 can you please indicate the exact steps (I need to know what is the json you're referring to)
Read full answer below βQuestion
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome
Reproducible demo link
no link
Describe the bug
How to reproduce the bug?
- ... use a demo json that can generate some component on the canvas, set an id in attributes
- ... use editor.setComponents(json)
- ... set style using editor.setStyle() function for the same id element What is the expected behavior? ... id shouldn't be changed while getting rendered on the canvas
What is the current behavior? ... id's are getting changed and because of which styles can't be implemented for the same
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (4)
Hi @shelendravashishtha2 can you please indicate the exact steps (I need to know what is the json you're referring to)
I've seen this happen to if you don't save a reference to the CSS before using the editor.setComponents() method All of the CSS markeup from the elements id selector/reference is removed when using editor.setComponents() method The exact steps i've encountered are save a reference of the editor components as JSON ie
`let jsonElements = editor.getComponents().toJSON();
editor.setComponents(jsonElements)
`
after using the setComponents method the CSS makrup is lost for all of the id selectors of the blocks, and some of the components id's will change ie if it was #iefzr it will change to #iefzr-2
The reason i've used saved the jsonElements is for having a way to edit them via the UI with an editor like monaco editor see my screenshot below, but even without modifying the JSON the CSS still get's reset
Is there a better way to go about modifying the JSON ie one to one instead of modifying it via setComponents?

I think I found the issue on my end using editor.getComponents().toJSON();
adds an empty "style":"" field to some of the components which is clearing out there CSS
ie
"components": [
{
"type": "box",
"style": "",
"attributes": {
"id": "ic7c"
}
},
{
"type": "box",
"style": "",
"attributes": {
"id": "if5j"
}
}
]Thanks for reporting this, @shelendravashishtha2.
Great question about Ids gets changed while trying to set components on the canvas using json. The recommended approach with StyleManager 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 can be achieved by listening to editor and component events
Common patterns:
// Listen for changes
editor.on('change', () => console.log('something changed'));
// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));
If you're still stuck:
- Share a minimal CodeSandbox reproduction
- Include what you've already tried
- Mention your GrapesJS version
- The community is here to help!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #6685
Custom component styles are not applied after deletion and re-addition
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v143.0.7499.193 Reproducible demo l...
Issue #5990
When you delete a component, duplicate classes between components will be deleted.
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome 126.0.6478.127(Windows) Reproducibl...
Issue #5229
textnode with content "null" is rendered as "null" not whitespace
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome Version 114.0.5735.199 (Official Buil...
Issue #4576
Clicking 'wrap for style' button affects the inner components of other components inside the same text box
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome 104.0.5112.101 Reproducible demo link...
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.