Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a rich text editor such as Quill or Draft.js with Next.js?

I have a big application that I'm building with Next.js for SEO and performance purposes, and there's a super interactive part of this application that needs a Text Editor (such as Quill.js or Draft.js) where data in it is synced between two users using Socket.io.

The problem is I can't get a Text Editor to work with Next.js.

When I import Quill, it says 'Document is not defined' because there's no document on the server. With Draft.js, it simply doesn't render anything but there's no errors.

Here's my code for Draft.js:

import { EditorState, convertToRaw, convertFromRaw } from 'draft-js'


class TextEditor extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editorState: EditorState.createWithContent(convertFromRaw(props.contentState))
    }
  }
  static async getInitialProps ({ query, req }) {
    return { contentState: convertToRaw(EditorState.createEmpty().getCurrentContent()) }
  }
  render() {
    console.log('init',this.props.editorState);
    return (
      <div>
        <h1>Test Editor</h1>
        <Editor
          editorState={ this.props.editorState }
        />
      </div>
    );
  }
}

Any help?

like image 440
Herbot Sikaro Avatar asked Mar 25 '19 15:03

Herbot Sikaro


People also ask

Can I add a rich text editor to a React project?

⚠️ Warning: Draft.js is a framework meant to be used with React, so if you already have a project you want to add a rich text editor to, but it’s written using a different library, like Vue, you may want to look at more suitable editor options. JefMari/awesome-wysiwyg is a great resource for looking at all your options.

What is a rich text editor?

Rich text editors enable you to control the appearance of the text and provide a powerful way for content creators to create and publish HTML anywhere. In this article, we will be using Draft.js and react-draft-wysiwyg to build a rich text editor and display the text we created using the editor.

How to add editor to a Div using quill?

First of all we need to instantiate a new Quill object and pass the id of the div where we want the editor to be as a parameter.

What is editorstate in react draft?

The Draft.js editor is built as a controlled ContentEditable component that is based on React’s controlled input API. EditorState provides a snapshot of the editor state.


1 Answers

Use sun editor. This one is really flexible and compatible with nextjs. https://www.npmjs.com/package/suneditor-react npm i suneditor suneditor-react

import dynamic from "next/dynamic";
import 'suneditor/dist/css/suneditor.min.css'; // Import Sun Editor's CSS File to the _app.js

const SunEditor = dynamic(() => import("suneditor-react"), { //besure to import dynamically
  ssr: false,
});

const MyComponent = props => {
  return (
    <div>
      <p> My Other Contents </p>
      <SunEditor />
    </div>
  );
};
export default MyComponent;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
like image 82
Mohammad Rahat Avatar answered Nov 01 '22 11:11

Mohammad Rahat