-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue: Multiple Instances of EditorJS Created in React with Vite and TypeScript #2731
Comments
Remove react strictmode to avoid double instances |
@Vishvsalvi , bro i am sorry to say but it's still not working, every single time when it re-rander it creates new instance of Editorjs |
I'm using hooks, it's working: import { useEffect, useRef, useState } from "react";
import EditorJS from "@editorjs/editorjs";
import { EditorConfig } from "@editorjs/editorjs/types/configs";
import { DEFAULT_EDITORJS_TOOLS } from "@/lib/editorjs/editorjs";
const useEditor = ({ onDestroy, ...config }: EditorConfig & { onDestroy?: () => void }) => {
const [isEditorReady, setIsEditorReady] = useState(false);
const editorInstance = useRef<EditorJS>(null);
useEffect(() => {
if (!editorInstance.current) {
let dom: HTMLElement | undefined = undefined;
if (config.holder instanceof HTMLElement) {
dom = config.holder;
} else if (config.holder) {
const el = document.getElementById(config.holder);
if (el) {
dom = el;
}
}
if (dom !== undefined) {
const editorJsDOM = document.createElement("div");
dom.appendChild(editorJsDOM);
editorInstance.current = new EditorJS({
tools: DEFAULT_EDITORJS_TOOLS,
...config,
holder: editorJsDOM,
onReady: () => {
setIsEditorReady(true);
config.onReady?.();
},
});
}
}
return () => {
if (editorInstance.current && editorInstance.current.destroy) {
editorInstance.current.destroy();
editorInstance.current = null;
onDestroy?.();
}
};
}, []);
return {
isEditorReady,
editor: editorInstance.current,
};
};
export default useEditor; |
@hsnfirdaus hello, bro can you share the full code with me, that would be verry helpfull, const useEditor = ({ onDestroy, ...config }: EditorConfig & { onDestroy?: () => void }) => {
const [isEditorReady, setIsEditorReady] = useState(false);
const editorInstance = useRef<EditorJS>(null); and please clearify this for me, where this |
@hsnfirdaus hi, can we talk in private. it would be helpfull if you can share your emil or something |
I'm using typescript, this is simplified example of my previous reply import { useEffect, useRef, useState } from "react";
import EditorJS from "@editorjs/editorjs";
import { EditorConfig } from "@editorjs/editorjs/types/configs";
const useEditor = (config: EditorConfig) => {
const [isEditorReady, setIsEditorReady] = useState(false);
const editorInstance = useRef<EditorJS>(null);
useEffect(() => {
if (!editorInstance.current) {
editorInstance.current = new EditorJS({
...config,
onReady: () => {
setIsEditorReady(true);
config.onReady?.();
},
});
}
return () => {
if (editorInstance.current && editorInstance.current.destroy) {
editorInstance.current.destroy();
editorInstance.current = null;
}
};
}, []);
return {
isEditorReady,
editor: editorInstance.current,
};
};
export default useEditor; Usage ( import { EditorConfig } from "@editorjs/editorjs/types/configs";
import Paragraph from "@editorjs/paragraph";
import Header from "@editorjs/header";
import useEditor from "./useEditor";
const config: EditorConfig = {
holder: 'editorjs',
tools: {
paragraph: Paragraph,
header: Header
}
}
function App() {
const { editor, isEditorReady } = useEditor(config);
return (
<div>
<div id="editorjs"></div>
</div>
)
}
export default App editor and isEditorReady variable is useful when you want to dynamicly change the content of editor (eg. After fetching content from server) |
@hsnfirdaus thanks bro, working perfectly, |
I am experiencing an issue with EditorJS in my React application using Vite and TypeScript. Specifically, multiple instances of the editor are being created upon the first mount, even though I intend to create only one instance and reuse it. Additionally, I'm encountering errors when attempting to destroy the editor instance.
Environment
EditorJS Version:
React Version:
Vite Version:
Steps to Reproduce
Code Snippets
editorConfig.ts:
Editor.tsx:
Expected Behavior
Actual Behavior
Additional Information
Device : lenove destop
Browser : chrome
OS: windows
The text was updated successfully, but these errors were encountered: