Issue #4196πŸ’¬ AnsweredOpened March 15, 2022by shelendravashishtha20 reactions

Ids gets changed while trying to set components on the canvas using json

Quick answerby artf

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?

  1. ... use a demo json that can generate some component on the canvas, set an id in attributes
  2. ... use editor.setComponents(json)
  3. ... 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)

artfβ€’ March 24, 2022

Hi @shelendravashishtha2 can you please indicate the exact steps (I need to know what is the json you're referring to)

JonathanRicheβ€’ March 25, 2022

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?

image

JonathanRicheβ€’ March 25, 2022
Code SnippetTEXT
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"
                }
            }
]
ClaudeCodeβ€’ May 17, 2026

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:

  1. Check the GrapesJS documentation for your specific module
  2. Look for the on() event listener method
  3. 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.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins β†’
Premium option

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.

All tutorials β†’

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.