Skip to content

Load pre-existing html into Editor via React custom hook #3208

Answered by chalecki
tolkienfan2 asked this question in Q&A
Discussion options

You must be logged in to vote

I faced the same challenge. I had to connect Lexical Editor as a Formik field. I end up creating a custom plugin 😎

import { $generateHtmlFromNodes, $generateNodesFromDOM } from '@lexical/html';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { CLEAR_HISTORY_COMMAND, $getRoot } from 'lexical';
import { useLayoutEffect } from 'react';

export const SetInitialValuePlugin: React.FC<{ initHtml: string }> = ({ initHtml = '' }) => {
  const [editor] = useLexicalComposerContext();

  useLayoutEffect(() => {
    if (editor && initHtml) {
      editor.update(() => {
        const content = $generateHtmlFromNodes(editor, null);

        if (!!initHtml && con…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by fantactuka
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants