gpt4 book ai didi

javascript - 未定义窗口 - react-draft-wysiwyg 与下一个 js (ssr) 一起使用

转载 作者:行者123 更新时间:2023-12-03 20:49:52 25 4
gpt4 key购买 nike

我正在开发一个富文本编辑器,用于将纯 html 转换为带有下一个 js 用于 ssr 的编辑器内容。我得到了这个错误窗口没有定义所以我搜索这个github的解决方案link
它使用了 next js 的动态导入功能。
而不是直接导入编辑器import { Editor } from "react-draft-wysiwyg";它使用此代码动态导入编辑器

const Editor = dynamic(
() => {
return import("react-draft-wysiwyg").then(mod => mod.Editor);
},
{ ssr: false }
);
但是尽管我已经安装了这个 react-draft-wysiwyg 模块,但我仍然收到此错误
ModuleParseError: Module parse failed: Unexpected token (19:9)
You may need an appropriate loader to handle this file type.
| import dynamic from "next/dynamic";
| var Editor = dynamic(function () {
> return import("react-draft-wysiwyg").then(function (mod) {
| return mod.Editor;
| });
这是我的全部代码
import React, { Component } from "react";
import { EditorState } from "draft-js";
// import { Editor } from "react-draft-wysiwyg";
import dynamic from "next/dynamic";

const Editor = dynamic(
() => {
return import("react-draft-wysiwyg").then(mod => mod.Editor);
},
{ ssr: false }
);

class MyEditor extends Component {
constructor(props) {
super(props);
this.state = { editorState: EditorState.createEmpty() };
}

onEditorStateChange = editorState => {
this.setState({ editorState });
};

render() {
const { editorState } = this.state;

return (
<div>
<Editor
editorState={editorState}
wrapperClassName="rich-editor demo-wrapper"
editorClassName="demo-editor"
onEditorStateChange={this.onEditorStateChange}
placeholder="The message goes here..."
/>
</div>
);
}
}

export default MyEditor;
请帮帮我。谢谢。

最佳答案

这是一个解决方法

import dynamic from 'next/dynamic'
import { EditorProps } from 'react-draft-wysiwyg'

// install @types/draft-js @types/react-draft-wysiwyg and @types/draft-js @types/react-draft-wysiwyg for types

const Editor = dynamic<EditorProps>(
() => import('react-draft-wysiwyg').then((mod) => mod.Editor),
{ ssr: false }
)

关于javascript - 未定义窗口 - react-draft-wysiwyg 与下一个 js (ssr) 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63451068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com