Suneditor React Component

3.6.1 · active · verified Sun Apr 19

suneditor-react provides a convenient React wrapper for the SunEditor WYSIWYG HTML editor. It simplifies integrating a rich text editor into React applications by encapsulating the underlying SunEditor library within a standard React component interface. The current stable version is 3.6.1, and the project appears to have an active release cadence with regular updates addressing bug fixes, performance improvements, and new features. A key differentiator is its explicit support for Next.js via dynamic imports to handle server-side rendering (SSR) compatibility. It also offers fine-grained control over editor configuration, including language settings, form name integration, and the ability to load only necessary plugins for performance optimization, differentiating it from simpler wrappers that might bundle all features by default. It requires the base `suneditor` package as a peer dependency, giving users control over its version.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to integrate `suneditor-react` into a Next.js application using dynamic imports, access the underlying SunEditor instance via a ref, set initial content, and handle content changes.

import React from 'react';
import dynamic from 'next/dynamic';
import 'suneditor/dist/css/suneditor.min.css'; // Import Sun Editor's CSS File

// Important: Dynamic import for Next.js to disable SSR
const SunEditor = dynamic(() => import('suneditor-react'), {
  ssr: false,
});

const MyRichTextEditor = () => {
  const editorRef = React.useRef();

  // The sunEditor parameter will be set to the core suneditor instance
  const getEditorInstance = (sunEditor) => {
    editorRef.current = sunEditor;
    if (editorRef.current) {
      console.log('SunEditor instance ready:', editorRef.current);
      // Example: Set initial content programmatically
      // editorRef.current.setContents('<p>Hello from <b>suneditor-react</b>!</p>');
    }
  };

  const handleEditorChange = (content) => {
    console.log('Editor content changed:', content);
  };

  return (
    <div>
      <h1>My Blog Post Editor</h1>
      <SunEditor
        getSunEditorInstance={getEditorInstance}
        onChange={handleEditorChange}
        setOptions={{
          height: '200px',
          buttonList: [['undo', 'redo'], ['font', 'fontSize', 'formatBlock'], ['bold', 'underline', 'italic', 'strike'], ['align', 'list'], ['image', 'link']],
          lang: 'en' // Example language setting
        }}
        defaultValue="<p>Start typing your content here...</p>"
      />
      <button onClick={() => console.log('Current content:', editorRef.current?.getContents())}>
        Log Current Content
      </button>
    </div>
  );
};

export default MyRichTextEditor;

view raw JSON →