Editor.js: editor.save() is not a function in React functional component

Created on 10 Nov 2019  路  5Comments  路  Source: codex-team/editor.js

Editorjs while imported in React functional component isn't working when calling function to save data.

const editor = new EditorJs({
    holder:'editorjs',
    placeholder: 'Let`s write an awesome story!',
    tools: {
        header : {
            class: Header,
            inlineToolbar:true,
            shortcut:"CTRL+SHIFT+H",
            config: {
                placeholder:'Enter Your text'
            }
        }
    },
    data:{}
})

const output = document.getElementById('output')

function Save() {
    editor.save().then(data => {
        output.innerHTML = data
    })
}

export default function Editor() {

    return (
        <div>
            <h1>Create Your Post</h1>
            <div id="editorjs" style={{border:'1px solid #ddd', marginBottom:'1rem'}} mx={4}></div>
            <Button variant="contained" color="primary" onClick={Save()}>Save Me</Button>
            <code id="output"></code>
        </div>
    )
}

Most helpful comment

This React component does not look alright to me.

  • You should initialise the Editor, after rendering your component.
  • onClick={Save()} is calling Save on render, not on click

it is calling Save function which have saving code.

onClick={Save} will call Save on click, but onClick={Save()} is going to call Save on render and nothing on click as Save is not returning anything

All 5 comments

This React component does not look alright to me.

  • You should initialise the Editor, after rendering your component.
  • onClick={Save()} is calling Save on render, not on click

This React component does not look alright to me.

  • You should initialise the Editor, after rendering your component.
  • onClick={Save()} is calling Save on render, not on click

it is calling Save function which have saving code.

and I also I want to contribute on this project, what should I need to do ?

I agreed with @mupkoo that your 'const editor = new EditorJs({holder:'editorjs',...' is executed before '

This React component does not look alright to me.

  • You should initialise the Editor, after rendering your component.
  • onClick={Save()} is calling Save on render, not on click

it is calling Save function which have saving code.

onClick={Save} will call Save on click, but onClick={Save()} is going to call Save on render and nothing on click as Save is not returning anything

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tiotdev picture tiotdev  路  3Comments

hata6502 picture hata6502  路  3Comments

vincentdesmares picture vincentdesmares  路  3Comments

guillaumepn picture guillaumepn  路  4Comments

davedbase picture davedbase  路  3Comments