question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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>
    )
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
mupkoocommented, Nov 13, 2019

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

2reactions
kenyxycommented, Nov 13, 2019

I agreed with @mupkoo that your ‘const editor = new EditorJs({holder:‘editorjs’,…’ is executed before ‘<div id=“editorjs”…’ is rendered, so not such element with the id can provide EditorJs’s constructor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React functional components- say its not a function
my functional component. const LiteraryPage = props => { if (Object.keys(mainData).
Read more >
State Management within React Functional Components with ...
For simple types, using const keyword protects the state from direct mutation. For object types, nothing stops you from updating the object's properties....
Read more >
Render Props - React
A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render...
Read more >
The Guide to Learning React Hooks (Examples & Tutorials)
Learn all about React Hooks with this hands-on guide. Includes tutorials and code examples on using hooks for state and effects, for context...
Read more >
React Functional Components, Props, and JSX - freeCodeCamp
If you revisit the example code at the top of this post, you'll see that the JSX code is being returned by a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found