lexical: add RefPlugin to set the ref
This commit is contained in:
18
src/features/compose/editor/plugins/ref-plugin.tsx
Normal file
18
src/features/compose/editor/plugins/ref-plugin.tsx
Normal file
@ -0,0 +1,18 @@
|
||||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
||||
import { LexicalEditor } from 'lexical';
|
||||
import React, { useEffect } from 'react';
|
||||
|
||||
/** Set the ref to the current Lexical editor instance. */
|
||||
const RefPlugin = React.forwardRef<LexicalEditor>((_props, ref) => {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
|
||||
useEffect(() => {
|
||||
if (ref && typeof ref !== 'function') {
|
||||
ref.current = editor;
|
||||
}
|
||||
}, [editor]);
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
export default RefPlugin;
|
||||
Reference in New Issue
Block a user