# LinwitDocEditor 与 React 整合 在 React 中,我们通过使用 `useRef` Hook 得到 dom 节点,然后再通过 `new LinwitDocEditor` 进行实例化,示例代码如下: ```jsx import {useEffect, useRef} from 'react' import {LinwitDocEditor} from "aieditor"; import "aieditor/dist/style.css" function App() { //定义 ref const divRef = useRef(null); //初始化 LinwitDocEditor useEffect(() => { if (divRef.current) { const aiEditor = new LinwitDocEditor({ element: divRef.current, placeholder: "点击输入内容...", content: 'LinwitDocEditor 是一个面向 AI 的开源富文本编辑器。 ', }) return ()=>{ aiEditor.destroy(); } } }, []) return ( <>

LinwitDocEditor,一个面向 AI 的富文本编辑器

) } export default App ``` ## React 组件 ```jsx // "use client"; // Next.JS import { LinwitDocEditor, AiEditorOptions } from "aieditor"; import "aieditor/dist/style.css"; import { HTMLAttributes, forwardRef, useEffect, useRef } from "react"; type AIEditorProps = Omit, "onChange"> & { placeholder?: string; defaultValue?: string; value?: string; onChange?: (val: string) => void; options?: Omit; }; export default forwardRef(function AIEditor( { placeholder, defaultValue, value, onChange, options, ...props }: AIEditorProps, ref ) { const divRef = useRef(null); const aiEditorRef = useRef(null); useEffect(() => { if (!divRef.current) return; if (!aiEditorRef.current) { const aiEditor = new LinwitDocEditor({ element: divRef.current, placeholder: placeholder, content: defaultValue, onChange: (ed) => { if (typeof onChange === "function") { onChange(ed.getMarkdown()); } }, ...options, }); aiEditorRef.current = aiEditor; } return () => { if (aiEditorRef.current) { aiEditorRef.current.destroy(); aiEditorRef.current = null; } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { if (ref) { if (typeof ref === "function") { ref(divRef.current); } else { ref.current = divRef.current; } } }, [ref]); useEffect(() => { if (aiEditorRef.current && value !== aiEditorRef.current.getMarkdown()) { aiEditorRef.current.setContent(value || ""); } }, [value]); return
; }); ``` ### 使用 ```jsx const [value, setValue] = useState(""); setValue(val)} /> ``` **在 `Next.JS` 中使用:** ```jsx const AIEditor = dynamic(() => import("./AIEditor"), { ssr: false, loading: () => , }); function App(){ const [value, setValue] = useState(""); return ( setValue(val)} />); } ``` 更多 react 集成示例请参考:https://gitee.com/aieditor-team/aieditor/tree/main/demos/react-ts