使用 react-rte 在 next js 中添加富文本编辑器

adding a rich text editor in next js with react-rte

我正在尝试在 nextjs 中添加富文本编辑器 我选择的是 react-rte 但我收到错误

Server Error
ReferenceError: window is not defined

我不知道我做错了什么我正在尝试实现一个简单的富文本编辑器 h1 h2 h3 h4 h5 h6 and paragragh tag

import React, { Component } from "react";
// import RichTextEditor from "react-rte";
import dynamic from "next/dynamic";
import PropTypes from "prop-types";
const RichTextEditor = dynamic(() => import("react-rte"), { ssr: false });

class MyStatefulEditor extends Component {
  static propTypes = {
    onChange: PropTypes.func,
  };

  state = {
    value: RichTextEditor.createEmptyValue(),
  };

  onChange = (value) => {
    this.setState({ value });
    if (this.props.onChange) {
      // Send the changes up to the parent component as an HTML string.
      // This is here to demonstrate using `.toString()` but in a real app it
      // would be better to avoid generating a string on each change.
      this.props.onChange(value.toString("html"));
    }
  };

  render() {
    return <RichTextEditor value={this.state.value} onChange={this.onChange} />;
  }
}

import dynamic from 'next/dynamic'
const RichTextEditor = dynamic(
  () => import('react-rte'),
  { ssr: false }
)

我正在使用 this playground for the demo

您可以通过一些评论查看此代码片段

import React, { Component } from "react";
import dynamic from "next/dynamic";
import PropTypes from "prop-types";
//import the component lazily (not module itself)
const RichTextEditor = dynamic(() => import("react-rte"), { ssr: false });

class MyStatefulEditor extends Component {
   constructor(props) {
       super(props)
       this.state = {}
   }

  static propTypes = {
    onChange: PropTypes.func,
  };

  //only trigger on the client-side
  componentDidMount = async () => {
    //import module on the client-side to get `createEmptyValue` instead of a component
    const module = await import("react-rte")
    this.setState({ 
        value: module.createEmptyValue(),
       })
  }

  onChange = (value) => {
    this.setState({ value });
    if (this.props.onChange) {
      this.props.onChange(value.toString("html"));
    }
  };

  render() {
    return <RichTextEditor value={this.state.value} onChange={this.onChange} />;
  }
}

export default MyStatefulEditor

Function-based component version

import React, { useState, useEffect } from "react";
import dynamic from "next/dynamic";
import PropTypes from "prop-types";
//import the component
const RichTextEditor = dynamic(() => import("react-rte"), { ssr: false });

const MyStatefulEditor = ({ onChange }) => {
  const [value, setValue] = useState();

  useEffect(() => {
    const importModule = async () => {
      //import module on the client-side to get `createEmptyValue` instead of a component
      const module = await import("react-rte");
      setValue(module.createEmptyValue());
    };
    importModule();
  }, []);

  const handleOnChange = (value) => {
    setValue(value);
    if (onChange) {
      onChange(value.toString("html"));
    }
  };

  return <RichTextEditor value={value} onChange={handleOnChange} />;
};

MyStatefulEditor.propTypes = {
  onChange: PropTypes.func
};

export default MyStatefulEditor;