Issue #3586✓ SolvedOpened July 2, 2021by jcsofts3 reactions

How to listen the components change in a component.

快速解答by dm-siegel2

I couldn't get this to work either. Error reading components() when setting the listenTo. The tag is empty (no contents inside) on init, so this makes sense. never mind... i was doing this in the init of the model instead of the view. this worked after I put it in the correct init :)

Read full answer below ↓

Question

init() { this.listenTo(this.model,'change:components', this.handleComponentsChange); },

I develop a component, I try to listen the child components change, then I add above code to view init, but the function doesn't fired when add/remove component.

how I can listen the components change in a component view?

thanks

Answers (4)

👍 Most helpfuldm-siegelApril 28, 2023

I couldn't get this to work either. Error reading components() when setting the listenTo. The tag is empty (no contents inside) on init, so this makes sense.

never mind... i was doing this in the init of the model instead of the view.

init() {
  const children = this.model.components();
  this.listenTo(children, 'add remove', this.handleComponentsChange);
},

this worked after I put it in the correct init :)

anlumoJuly 8, 2021

The components collection doesn't change, that's why there's no event triggered.

The storage backend is a Backbone collection, see here. You have to listen to its events, specifically add and remove.

artfJuly 27, 2021

As already mentioned by Andreas, you have to add a listener to the collection.

init() {
  const children = this.model.components();
  this.listenTo(children, 'add remove', this.handleComponentsChange);
},
ClaudeCodeMay 17, 2026

Thanks for reporting this, @jcsofts.

Great question about how to listen the components change in a component.. The recommended approach with Components 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.