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?
⚠️ 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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With