在NextJS 13中使用@uiw/react-md-editor报错,求解决方案
解决Next.js 13中@uiw/react-md-editor的报错问题
根据你的代码和使用场景,以下是针对性的修复步骤:
1. 标记组件为客户端组件
在App Router模式下,Next.js默认所有组件为服务端组件,而@uiw/react-md-editor依赖浏览器API,必须显式声明为客户端组件。修改editor.tsx,在文件顶部添加指令:
'use client'; // 必须添加这一行 import dynamic from 'next/dynamic'; import { MDEditorProps } from '@uiw/react-md-editor'; const MDEditor = dynamic<MDEditorProps>(() => import('@uiw/react-md-editor'), { ssr: false, }); export const BlogList = (props: BlogListProps) => { const { heading, listContents, data } = props; const styles = useStyles(); return ( <div> <MDEditor value={data} height={700} /> </div> ); };
2. 调整Next.js配置(移除冲突插件)
next-remove-imports插件可能误删@uiw/react-md-editor所需的样式导入,导致报错。修改next.config.ts,移除该插件:
const { withNx } = require('@nrwl/next/plugins/with-nx'); /** * @type {import('@nrwl/next/plugins/with-nx').WithNxOptions} **/ const nextConfig = { nx: { svgr: false, }, experimental: { appDir: true, }, }; module.exports = withNx(nextConfig);
3. 验证依赖版本兼容性
确保@uiw/react-md-editor版本适配Next.js 13,安装最新稳定版:
npm install @uiw/react-md-editor@latest # 或使用yarn yarn add @uiw/react-md-editor@latest
额外排查点
如果上述步骤未解决问题,检查以下内容:
- 确认
data变量为字符串类型,符合MDEditor的value参数要求 - 清除开发环境缓存:删除
.next文件夹和node_modules/.cache,重启项目
内容的提问来源于stack exchange,提问作者Nelson Vu
相关产品推荐
相关产品推荐

