xdan / jodit

Jodit - Best WYSIWYG Editor for You
https://xdsoft.net/jodit/
MIT License
1.61k stars 341 forks source link

Unhandled Runtime Error, TypeError: nextCreate is not a function #1127

Closed yhoungdev closed 1 month ago

yhoungdev commented 2 months ago

Jodit Version: 3.4.xxxxx

Browser: Brave OS: Linux Is React App: False

Code

Your Example code below is wrong, Next Js would throw an error, due to wrong use of the useMemo Hook

Screenshot from 2024-05-02 14-26-36

// import React, { useState, useRef, useMemo } from 'react';
import JoditEditor from 'jodit-react';

const Example = ({ placeholder }) => {
    const editor = useRef(null);
    const [content, setContent] = useState('');

    const config = useMemo(
        {
            readonly: false, // all options from https://xdsoft.net/jodit/docs/,
            placeholder: placeholder || 'Start typings...'
        },
        [placeholder]
    );

    return (
        <JoditEditor
            ref={editor}
            value={content}
            config={config}
            tabIndex={1} // tabIndex of textarea
            onBlur={(newContent) => setContent(newContent)} // preferred to use only this option to update the content for performance reasons
            onChange={(newContent) => {}}
        />
    );
};

Expected behavior: useMemo expects a function, but an object was passed, so there for it throws this error in NextJs Unhandled Runtime Error, TypeError: nextCreate is not a function

Actual behavior:

So this is an upgrded code that works for both React and NextJs

import React, { useState, useRef, useMemo } from 'react';
import JoditEditor from 'jodit-react';

const Example = ({ placeholder }) => {
    const editor = useRef(null);
    const [content, setContent] = useState('');

    const config = useMemo(()=> ({
            readonly: false, // all options from https://xdsoft.net/jodit/docs/,
            placeholder: placeholder || 'Start typings...'
        }),[placeholder]
    );

    return (
        <JoditEditor
            ref={editor}
            value={content}
            config={config}
            tabIndex={1} // tabIndex of textarea
            onBlur={(newContent) => setContent(newContent)} // preferred to use only this option to update the content for performance reasons
            onChange={(newContent) => {}}
        />
    );
};
yhoungdev commented 1 month ago

Awesome