ReferenceError: self is not defined while using CKEditor [duplicate]
Asked Answered
M

1

11

ReferenceError: self is not defined while importing CKEditor. I am using next.js.

import { CKEditor } from '@ckeditor/ckeditor5-react';

Already installed using

npm install --save @ckeditor/ckeditor5-react @ckeditor/ckeditor5-build-classic
Moue answered 17/6, 2022 at 10:55 Comment(2)
This seems like an issue with Webpack you might have to add the CKEditor webpack config plugin to next.jsMolt
You should dynamically import CKEditor using next/dynamic with ssr: false to avoid loading it on the server-side, thus preventing the error from occuring.Urethrectomy
I
23

After some trial-and-error I have finally made it work with this setup (NextJS project). No need to alter webpack config. How you handle your input data is up to you.

-MyEditor.jsx

import ClassicEditor from "@ckeditor/ckeditor5-build-classic";
import { CKEditor } from "@ckeditor/ckeditor5-react";
import React from "react";

const Editor = ({
  value,
  onChange,
}) => {
  return (
    <CKEditor
      editor={ClassicEditor}
      data={value}
      onChange={(event, editor) => {
        const data = editor.getData();
        onChange(data);
      }}
    />
  );
};

export default Editor;

-in any other component:

import dynamic from "next/dynamic";

const MyComp = () => {
   const Editor = dynamic(() => import("./MyEditor"), { ssr: false });
   return (
     <Editor            
        value={"Foo"}
        onChange={(v) => console.log(v)}
     />
)};
Inveterate answered 30/6, 2022 at 9:5 Comment(4)
the error gone but it is not working as expectedPreference
dynamic import saved my brain from explodingContemptuous
@Preference you can define all functions like onChange. (onBlur, onFocus). And you can access them like in the answer is onChangeAnsley
you should put dynamic import inside a useMemo, so it wont reimport the component on every render. cosnt TextEditor = useMemo(() => dynamic(() => import("@/components/UI/input/TextEditor"), { ssr: false }), []);Salyer

© 2022 - 2024 — McMap. All rights reserved.