Issue #6143💬 AnsweredOpened September 10, 2024by rhoenerSBS0 reactions

BUG/[QUESTION]: How to correctly wrap a Component on add/mount?

快速解答by jasonvijayy

Hi @artf, Reproducible Link I’m also hitting a crash when using component.replaceWith() together with UndoManager. This only happens when replaceWith() is triggered during editor lifecycle events (e.g. change:status, component:selected, component:deselected). On undo, GrapesJS tries to restore selection/state for the...

Read full answer below ↓

Question

GrapesJS version

  • I confirm to use the latest version of GrapesJS

What browser are you using?

Chrome v128

Reproducible demo link

https://jsfiddle.net/fjyk6n4a/2/

Describe the bug

Hi @artf, I am currently trying to wrap a component in a section component if its has not been wrapped yet. I am hooking into the component:mount event and replacing the added component with the section component (which has the added one as child). My Code looks something like this:

editor.on('component:mount', (component) => {
       if (component.parent().attributes.tagName !== 'body')
            return;
        if (component.is('section'))
            return;
        component.replaceWith({
            type: 'section',
            components: component
        });
})

The issue I'm facing is that the UndoManager stack is not completely aligned with what is happening. After the add and wrap the stack has 3 Entries: remove of original component, add of section and add of original component. When clicking undo the section is removed and the original component stays. But it is buggy in means of it is not removable and not really there because the stack entries do not reflect the changes correctly (maybe missing the initial add).

How to reproduce what I described:

  1. open reproducable demo link
  2. add text component (is wrapped in section as visible in layers)
  3. click ctrl+z to undo
  4. text is still in canvas and layers, section is gone. text is not removable

My Question: Ideally I would like to change the UndoManager stack in that on undo either the section and original component are both removed at the same time and without bugs (currently the problem) or the original component is removed and if you click undo again then the section is removed (as if the section was added first). In component:mount event the stack does not have the newest entry of the add yet so I cant manipulate it at that time. What is the best way to wrap the Component without having problems with the UndoManager or how can I manipulate the stack to fix it?

Code of Conduct

  • I agree to follow this project's Code of Conduct

Answers (2)

jasonvijayyJanuary 30, 2026

Hi @artf,

Reproducible Link

I’m also hitting a crash when using component.replaceWith() together with UndoManager.

This only happens when replaceWith() is triggered during editor lifecycle events (e.g. change:status, component:selected, component:deselected).

On undo, GrapesJS tries to restore selection/state for the old component, but it no longer exists in the parent collection, which leads to errors like:

Cannot read properties of undefined (reading 'indexOf')

The same replaceWith() logic works fine when executed from Trait.

Is there a recommended, undo-safe way to replace a component during selection changes.

Thanks.

ClaudeCodeMay 17, 2026

Thanks for reporting this, @rhoenerSBS.

Great question about BUG/[QUESTION]: How to correctly wrap a Component on add/mount?. The recommended approach with Canvas 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.