Skip to content
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

Multiple instance initialized #202

Open
devmnj opened this issue May 23, 2022 · 4 comments
Open

Multiple instance initialized #202

devmnj opened this issue May 23, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@devmnj
Copy link

devmnj commented May 23, 2022

Environment

  • @editorjs/editorjs version: x.y.z
  • react-editor-js version: x.y.z

Describe

Multiple instance of the Editojs appeared. Following are my package json

"dependencies": { "@testing-library/jest-dom": "^5.16.4", "@testing-library/react": "^13.2.0", "@testing-library/user-event": "^13.5.0", "react": "^18.1.0", "react-dom": "^18.1.0", "react-scripts": "5.0.1", "web-vitals": "^2.1.4", "@editorjs/checklist": "1.1.0", "@editorjs/code": "2.4.1", "@editorjs/delimiter": "1.1.0", "@editorjs/editorjs": "2.15.1", "@editorjs/embed": "2.2.1", "@editorjs/header": "2.3.0", "@editorjs/image": "2.3.1", "@editorjs/inline-code": "1.3.0", "@editorjs/link": "2.1.3", "@editorjs/list": "1.4.0", "@editorjs/marker": "1.2.0", "@editorjs/paragraph": "2.6.0", "@editorjs/quote": "2.3.0", "@editorjs/raw": "2.1.1", "@editorjs/simple-image": "1.3.2", "@editorjs/table": "1.2.0", "@editorjs/warning": "1.1.1", "react-editor-js": "1.4.1"

Editor component

`import ReactDOM from "react-dom";
import React, { Component } from "react";

import {createReactEditorJS} from "react-editor-js";

import { EDITOR_JS_TOOLS } from "./constants";
const EditorJs = createReactEditorJS();

export default class BlockEditor extends Component {
render() {
return (
<EditorJs
tools={EDITOR_JS_TOOLS}
data={{
time: 1556098174501,
blocks: [
{
type: "header",
data: {
text: "Editor.js",
level: 2
}
},
{
type: "paragraph",
data: {
text:
"Hey. Meet the new Editor. On this page you can see it in action — try to edit this text."
}
},
{
type: "header",
data: {
text: "Key features",
level: 3
}
},
{
type: "list",
data: {
style: "unordered",
items: [
"It is a block-styled editor",
"It returns clean data output in JSON",
"Designed to be extendable and pluggable with a simple API"
]
}
},
{
type: "header",
data: {
text: "What does it mean «block-styled editor»",
level: 3
}
},
{
type: "paragraph",
data: {
text:
'Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core.'
}
},
{
type: "paragraph",
data: {
text:
'There are dozens of ready-to-use Blocks and the simple API for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.'
}
},
{
type: "header",
data: {
text: "What does it mean clean data output",
level: 3
}
},
{
type: "paragraph",
data: {
text:
"Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below"
}
},
{
type: "paragraph",
data: {
text:
'Given data can be used as you want: render with HTML for Web clients, render natively for mobile apps, create markup for Facebook Instant Articles or Google AMP, generate an audio version and so on.'
}
},
{
type: "paragraph",
data: {
text:
"Clean data is useful to sanitize, validate and process on the backend."
}
},
{
type: "delimiter",
data: {}
},
{
type: "paragraph",
data: {
text:
"We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make it's core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏"
}
},
{
type: "image",
data: {
file: {
url:
"https://codex.so/upload/redactor_images/o_e48549d1855c7fc1807308dd14990126.jpg"
},
caption: "",
withBorder: true,
stretched: false,
withBackground: false
}
}
],
version: "2.12.4"
}}
/>
);
}
}

`

App block

import './App.css';
import BlockEditor from './components/BlockEditor'

function App() {
return (




);
}

react-editor-js

@devmnj devmnj added the bug Something isn't working label May 23, 2022
@Jungwoo-An
Copy link
Owner

@devmnj Hi! First of all, thanks for your interest! 👋

Could you upgrade react-editor-js to latest version?

Thanks!

@devmnj
Copy link
Author

devmnj commented Jun 5, 2022

I tried @Jungwoo-An I tried. Here is the sandbox

@zanzlender
Copy link

zanzlender commented Aug 3, 2022

Same thing happening to me with React, although I thought it might be because of Concurrent rendering released in React 18.0+.

Also. I have a question maybe related to this... I'm still rather new to React and all of this, but I am more comfortable with functional programming... Since React docs also recommends using that way because of some new features, my question is, is it possible to rewrite this class component into a functional, but so that it works with EditorJS?

I would like to take up that challenge for myself and the hopefully the community, but before that I want to know is it even possible... Like is it impossible to write it as a functional because of the way EditorJS is written... so I don't waste a week trying to do something... 👍

@zanzlender
Copy link

zanzlender commented Aug 3, 2022

Double render is fixed by making small changes to the example in README.

=> wrap InitEditor in use callback

const isInstance = useRef<EditorJS | null>(null)

const initEditor = useCallback(() => { ... }, [isInstance]);

=> use a Ref to check if initial render, because of concurrent rendering React 18.0, useEffect now triggers twice on every render

const firstRenderRef = useRef(true);

useEffect(() => {
  if (!isInstance.current && firstRenderRef.current) {
    firstRenderRef.current = false;
    initEditor();
  }
  return () => {
    if (isInstance.current) {
      isInstance.current.destroy()
      isInstance.current = null;
    }
  } 
}, [isInstance, firstRenderRef, initEditor]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants