Editor Destroy method throws an error in React
Thanks for reporting this, @Townsheriff. Great question about Editor Destroy method throws an error in React. The recommended approach with Components 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 ach...
Read full answer below ↓Question
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome 98
Reproducible demo link
https://codesandbox.io/s/withered-rain-z74rst?file=/src/App.js
Describe the bug
How to reproduce the bug?
- Unmount child element
- Create useEffect with callback where editor is destroyed
What is the expected behavior? No error
What is the current behavior?
Throws error, because cannot find an element. At least a getBody() returns null.
Stack trace from fork 0.18.3 with disabled minifaction.
TypeError: Cannot read properties of null (reading 'querySelector')
at child.getBody (grapes.min.js:53052:1)
at grapes.min.js:11286:1
at Array.forEach (<anonymous>)
at child.toggleSelectComponent (grapes.min.js:11284:1)
at child.stopSelectComponent (grapes.min.js:11244:1)
at child.stop (grapes.min.js:11975:1)
at EditorModel.stopDefault (grapes.min.js:46802:1)
at EditorModel.destroyAll (grapes.min.js:46938:1)
at Object.destroy (grapes.min.js:47790:1)
at page-builder-tab.tsx:28:16
In case code sandbox disappears:
import grapesjs from "grapesjs";
import React from "react";
export default function App() {
const [showFoo, setShowFoo] = React.useState(true);
const toggle = () => {
setShowFoo(!showFoo);
};
return (
<div>
<button onClick={toggle}>Click To Toggle!</button>
{showFoo ? <Foo /> : <p>Click Button To Show Grapes</p>}
</div>
);
}
const Foo = () => {
React.useEffect(() => {
const editor = grapesjs.init({
container: "#gjs",
fromElement: 1,
storageManager: { type: 0 },
plugins: ["gjs-blocks-basic"]
});
return () => {
editor.destroy();
};
});
return <div id="gjs"></div>;
};
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (1)
Thanks for reporting this, @Townsheriff.
Great question about Editor Destroy method throws an error in React. The recommended approach with Components 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 #5019
Can't set preview mode on editor init when using loadProjectData()
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v111 Reproducible demo link https:/...
Issue #6494
Create and destroy repeatedly throws error
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Reproducible demo link https://code...
Issue #4191
We are trying to store the data from grapesJS editor in Angular 10 application, getting CORS error, image attached below
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Chrome 99.0.4844.5 Reproducible demo link...
Issue #5720
Propagating component defaults can break layers when child comments are introduced
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v122 Reproducible demo link https:/...
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
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
Tutorial
Preset DevFuture 2.0 — New Update Released
Preset continues evolving as a clean, production-ready starting point for building visual editors powered by GrapesJS
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.