reactjsmaterial-uirte

MUI-RTE Getting the input content as string with HTML tags


I have react based app with mui components and in this particular case I'm stuck on RTE. The flow is:

The problem is i can't figure out how to get the content of the RTE like string with all the tags inside of it, it always turns up with some strange response and i can't parse it or stringify it properly...

  const sample = '<h1>Hello, {{name}}!</h1><p>this is test</p>';
  const contentHTML = convertFromHTML(sample);
  const state = ContentState.createFromBlockArray(
    contentHTML.contentBlocks,
    contentHTML.entityMap,
  );
  const content = JSON.stringify(convertToRaw(state));

  <MUIRichTextEditor
    label=""
    defaultValue={content}
    //onSave={}
    // or
    //onChange={}
  />

Solution

  • try using draft-js-export-html it has worked for me

    import { stateToHTML } from 'draft-js-export-html';
    
    return ( 
     <YourComp 
       onChange={ value => stateToHTML(value.getCurrentContent()) }
      />
    )