1

I want to create text editor in React.js app, using react-editor-js. However I cannot get this module working on a functional component. My code roughly looks like this:

import { createReactEditorJS } from "react-editor-js";
import { EDITOR_JS_TOOLS } from "./EditorConstants";

const ReactEditorJS = createReactEditorJS();

const CreateNewPost = () => {
    const { t } = useTranslation();
    const [editorValue, setEditorValue] = useState({
        time: new Date().getTime(),
        blocks: [
            {
                type: "header",
                data: {
                    text: "Testing title",
                    level: 2
                }
            },
            {
                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. 😏"
                }
            },

        ],
        version: "2.1.0"
    });

    return (
        <div className={style.wrapper}>
            <div className={style.container}>

                {/*TITLE*/}
                <div className={style.titleContainer}>
                    <label>{t("post.postTitle")}</label>
                    <input type="text"/>
                </div>

                <div>
                    <ReactEditorJS
                        tools={EDITOR_JS_TOOLS}
                        data={editorValue}
                    />
                </div>

            </div>
        </div>
    )
}

export default CreateNewPost;

The result - there is no any editor on the page. How to make it working?

This example uses class component and editor is working. While the same will not work on a functional component.

2 Answers 2

4

For anyone experiencing similar issue where Editor JS content is rendered empty, disabling strict mode works. Remove React.StrictMode enclosure tags from App.jsx or App.tsx file.

//From this

root.render(
  <React.StrictMode>
    <BrowserRouter basename='admin'>
      <App />
    </BrowserRouter>
  </React.StrictMode>
);

//Change to this

root.render(
    <BrowserRouter basename='admin'>
      <App />
    </BrowserRouter>
);
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think that should be the solution. Your root cause of the problem here is the regulations in react 18. the react wrapper around editorjs github.com/Jungwoo-An/react-editor-js that you used is not working very well in react 18. see the issues in github: github.com/Jungwoo-An/react-editor-js/issues/228. I am not sure why this is happening but the library is not getting any new commits almost a year.
0

Your code should work, something outside of it or the way you are rendering the component is probably the issue.

Here's a working example using a functional component:

CreateNewPost.js

import { useState } from "react";
import { createReactEditorJS } from "react-editor-js";
import { EDITOR_JS_TOOLS } from "./constants";

const ReactEditorJS = createReactEditorJS();

const CreateNewPost = () => {
  const [editorValue, setEditorValue] = useState({
    time: new Date().getTime(),
    blocks: [
      {
        type: "header",
        data: {
          text: "Testing title",
          level: 2
        }
      },
      {
        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. 😏"
        }
      }
    ],
    version: "2.1.0"
  });

  return <ReactEditorJS tools={EDITOR_JS_TOOLS} data={editorValue} />;
};

export default CreateNewPost;

index.js

import React from "react";
import { createRoot } from "react-dom/client";
import CreateNewPost from "./CreateNewPost";

const container = document.getElementById("app");
const root = createRoot(container);
root.render(<CreateNewPost />);

Edit react-editor-js-v2 (forked)

3 Comments

This works, but in this way i cannot export the componenet and have to put the code for the editor inside index.js .
@A.Aziz Example updated to show how you can export the component.
How do I get the value OnChange?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.