Changes in component's attributes being reproduced in all the instances instead of just one
Thanks @rmadeiraneto for the report. Yeah, unfortunately that's an issue if you're mutating arrays/objects properties in that way and to avoid that you have 2 options:Avoid direct mutations (assign new references when you have to update them)Define defaults as a function But unfortunately... the second options doesn't...
Read full answer below βQuestion
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome v114
Reproducible demo link
https://jsfiddle.net/rmadeiraneto/t659usxv/39/
Describe the bug
How to reproduce the bug?
- Go to the blocks list and drag the custom component "example" to the canvas
- Three elements that are coming from a default attribute (array) are displayed in the canvas
- Select the "Example" component
- Go to the traits panel and click on the plus icon a couple of times
- This would add items to the array that is in the attributes of the Example component and draw them in the page
- Drag another instance of the Example block into the canvas
- Notice that the items that you added to the array are being also displayed in the new instance of the component
What is the expected behavior?
When creating the second instance of the same component after changing the attributes of the first one, it's expected that the attributes of the second component would be the first array defined in the defaults [1,2,3] under customArray prop, instead of the manipulated array [1,2,3,4,5].
What is the current behavior?
When we change the attributes of an instance of a component, since the reference to the default object is probably being preserved, we are also changing the defaults of the component, making our changes on a single component instance being reproduced in all instances of the same component. When GrapesJs assigns custom defaults to components' attributes, it needs to create a shallow copy of the objects. When making the same thing with strings or integers, instead of objects or arrays, the issue is not verified, that's why I think it is a matter of keeping the reference for the defaults' objects when assigning the component's attributes.
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (3)
Thanks @rmadeiraneto for the report. Yeah, unfortunately that's an issue if you're mutating arrays/objects properties in that way and to avoid that you have 2 options:
- Avoid direct mutations (assign new references when you have to update them)
- Define
defaultsas a function
defaults: () => ({ customArray: [1,2,3] }),
But unfortunately... the second options doesn't work right now π I'll fix it in the next release
@artf using defaults as a function works, thanks for quick response and for providing the alternative fix on this issue. About the first suggestion, I think it's not about people should mutate or not the object, because we're talking about using the provided method (set). I'm sorry I didn't have the time to look at the source code, but you're probably mutating the defaults object inside the 'set' method, probably because the reference comes from early on in the component lifecycle. Again, without looking into the code, I assume that when you merge the defaults with the attributes of the component, you're keeping the reference to the defaults object instead of using a merge method that would return a copy instead, for example, the from immutable. I'll try to find some time to look into the code and suggest something more specific
Again, thanks for the alternative and for this amazing library, keep up the great work!
Thanks for reporting this, @rmadeiraneto.
Great suggestion about Changes in component's attributes being reproduced in all the instances instead of just one! While this specific feature isn't yet in the core API, there are several ways to achieve similar behavior.
Using the event system:
editor.on('component:update', (component) => {
// your logic here
});
Alternative approaches:
- Listen to
selector:addfor CSS selector changes - Use
selector:customfor custom rules - Tap into the
change:*events for fine-grained tracking - Build a plugin that extends the editor with this capability
Making it official: If this feature would benefit many users, consider opening a formal Feature Request on the GrapesJS repo with:
- A detailed use case
- Code example showing the desired behavior
- Why this matters for your workflow
The core team is receptive to well-motivated feature requests backed by real use cases.
Related Questions and Answers
Continue research with similar issue discussions.
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...
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 #5460
Loading `component` and `styles` from `pageManager`
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome v117.0.5938.149Reproducible demo link...
Issue #6102
Traits Not Displayed After Reloading Editor for Custom Component
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome 91VReproducible demo link https://jsf...
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.