Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditorJS on React

So I wanted to check out this new rich text editor Editor, https://editorjs.io/

I installed the unofficial reactJS version, but I'm not quite sure what I'm doing wrong here... https://www.npmjs.com/package/react-editor-js

Has any used this before? can it be done with hooks? my thinking is that I need to define an instance of this editor so I can save the data. because currently onChange the editor is not adding any new blocks to the data object or the entered data.

enter image description here

also, if I passed the data object as an empty object in the console does not show an initial EditorJs block.

any help would be appreciated.

function App() {
  let data = { '1': 'test' }

  return (
    <div className="App">
      <EditorJs
        data={data}
        onChange={(e) => console.log(data)}
        tools={{
          code: Code,
          header: Header,
          paragraph: Paragraph
        }}
      />
    </div>
  );
}
like image 286
Lee Avatar asked Mar 08 '20 00:03

Lee


People also ask

Can I use editor js in react?

Editor. js is is not a react component. So we can create a wrapper component that takes care of managing the life cycle of editor.

What is the best editor for React js?

We consider WebStorm to be the best-paid IDE on the market for React development, thanks to its many features, plugins, and good documentation.

How do I use editor in react in Monaco?

The easiest way to use the react-monaco-editor with create-react-app is to use the react-app-rewired project. For setting it up, the following steps are required: Install react-app-rewired : npm install -D react-app-rewired. Replace react-scripts by react-app-rewired in the scripts section of your packages.

What is Draftjs?

Draft. js is a framework for building rich text editors in React, powered by an immutable model and abstracting over cross-browser differences. Draft.


Video Answer


1 Answers

You can do this with hooks, write it like this:

const YourComponent = () => {
  const instanceRef = useRef(null)

  async function handleSave() {
    const savedData = await instanceRef.current.save()
    console.log(savedData)
   }

And when you put the component in the return function do it like this:

        <EditorJS
          instanceRef={(instance) => (instanceRef.current = instance)}
          tools={EDITOR_JS_TOOLS}
          data={data}
        />

And don't forget to import useRef from react

like image 195
Pitis Avatar answered Oct 19 '22 21:10

Pitis