Event handler on view not bound to the component
The error here is using the arrow function, just replace it with clickOnElement() {
Read full answer below βQuestion
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome Version 115.0.5790.114
Reproducible demo link
https://jsfiddle.net/mcraf3se/5/
Describe the bug
Hello, just starting with GrapesJS so it is quite likely I am misunderstanding something fundamental. I am struggling with setting the event handlers on the view of a custom component.
How to reproduce the bug? In the linked JS fiddle.
- Drag block My Div onto canvas
- Click inside it (there should be a div with 'stuff').
What is the expected behavior?
Per documentation here the event handlers should be bound to the component and I should be able to use something like this.model. ....
What is the current behavior? When running locally the handler is bound to window. In the JSFilddle this seems to be undefined. In both cases call to this.model.components fails
If is necessary to execute some code in order to reproduce the bug, paste it here below:
# the fiddle should have the code in it but if someone wants to run it locally. Here is the code.
const editor = grapesjs.init({
// Indicate where to init the editor. You can also pass an HTMLElement
container: '#gjs',
// Get the content for the canvas directly from the element
// As an alternative we could use: `components: '<h1>Hello World Component!</h1>'`,
fromElement: true,
// Size of the editor
height: '300px',
width: '100%',
// Disable the storage manager for the moment
storageManager: false,
// Avoid any default panel
panels: { defaults: [] },
blockManager: {
appendTo: '#blocks',
blocks: [
{
id: 'my-div',
label: 'My Div',
content: {
type: 'my-input-type'
}
},
]
},
});
editor.DomComponents.addType('my-input-type', {
// Make the editor understand when to bind `my-input-type`
isComponent: el => el.tagName === 'div',
// Model definition
model: {
defaults: {
tagName: 'div',
// draggable: 'form, form *', // Can be dropped only inside `form` elements
content: 'Stuff',
droppable: false, // Can't drop other elements inside
}
},
view: {
tagName: 'div',
events: {
click: 'clickOnElement'
},
clickOnElement: () => {
console.log(this)
this.model.components('hello')
}
}
});
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (2)
The error here is using the arrow function, just replace it with clickOnElement() {
Thanks for reporting this, @fluke777.
Great question about Event handler on view not bound to the component. The recommended approach with Canvas 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 #5705
Component tools misalign when the editor is resized
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v122 Reproducible demo link https:/...
Issue #6269
Component model and view inits every time it's dragged over every component present in Canvas on which it can be dropped into
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Version 130.0.6723.59 (Official Bui...
Issue #5316
Editor not subscribing to selector:remove
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome v115.0.5790.170 Reproducible demo lin...
Issue #6458
sector.setName doesn't work
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Reproducible demo link https://jsfi...
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
GrapesJS vs Webflow vs Builder.io vs Puck: Which Visual Builder to Choose in 2026
A practitioner's 2026 comparison of GrapesJS, Webflow, Builder.io, and Puck β pricing, AI features, lock-in, and a one-line rule for picking the right one
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
Why use grapesjs shadcn for your template builder
Skip months of editor plumbing and start with a fully working visual builder β polished UI included.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.