html-to-draftjs icon indicating copy to clipboard operation
html-to-draftjs copied to clipboard

html-to-draftjs in next.js TypeError: htmlToDraft is not a function

Open harshitsan opened this issue 5 years ago • 15 comments

  1. So if I import htmlToDraft like this import htmlToDraft from "html-to-draftjs";

  2. I am getting error Window is not defined So I did const htmlToDraft = dynamic( () => import("draftjs-to-html").then((mod) => mod.htmlToDraft), { ssr: false } );

  3. Then error is not coming but when I use it like `useEffect(() => { props.getDescription().then((res) => { // console.log(res.data); const blocksFromHtml = htmlToDraft(res.data); const { contentBlocks, entityMap } = blocksFromHtml; const contentState = ContentState.createFromBlockArray( contentBlocks, entityMap ); setEditor(EditorState.createWithContent(contentState));

    }); }, []);`

Error is coming: TypeError: htmlToDraft is not a function

@jpuri Can you help me?

harshitsan avatar Jun 14 '20 06:06 harshitsan

Same problem in [email protected]

AnDyro751 avatar Jul 05 '20 06:07 AnDyro751

any solution? I have the same problem

dulcelariz avatar Jul 16 '20 17:07 dulcelariz

I have the same problem too :-(

subodhk01 avatar Jul 25 '20 10:07 subodhk01

One alternative is using JSON format instead of HTML. And that JSON data can then be converted to HTML whenever required.

harshitsan avatar Jul 26 '20 10:07 harshitsan

What I did was use version 1.4.0 and directly import with import htmlToDraft from "html-to-draftjs";

subodhk01 avatar Jul 26 '20 10:07 subodhk01

I have the same problem with Next.js (apparently so does everyone else). Neither direct import works, nor using the Dynamic import.

OssiPesonen avatar Aug 16 '20 13:08 OssiPesonen

@OssiPesonen Switching to version 1.4.0 worked for me

subodhk01 avatar Aug 16 '20 13:08 subodhk01

@OssiPesonen Switching to version 1.4.0 worked for me

Yes, downgrading back from 1.5.0 to 1.4.0 worked. Thanks!

OssiPesonen avatar Aug 16 '20 13:08 OssiPesonen

Downgrading worked. Can confirm. Use 1.4.0.

kumarabhirup avatar Aug 20 '20 11:08 kumarabhirup

1.4.0 worked without dynamic import in development. Throwing "window is not defined" in production.

RehanS12 avatar Aug 25 '20 14:08 RehanS12

You can load as below:

useEffect(() => {
    htmlToDraft = require('html-to-draftjs').default
  }, [])

ducbtsn avatar Mar 19 '21 09:03 ducbtsn

I use version 1.5.0. package.json has this "html-to-draftjs": "^1.5.0",

I use like this in nextjs and its working (Note: nextjs dynamic didn't work for me)

Top of the page, just below imports

let htmlToDraft = null;
if (typeof window === 'object') {
  htmlToDraft = require('html-to-draftjs').default;
}

kiranvj avatar May 09 '21 06:05 kiranvj

Nextjs 12* with TS

I downgraded to v1.4.0 and directly imported with import htmlToDraft from "html-to-draftjs;

and after

const EditorComponent = () => {
...

let contentBlock: any;

if(process.browser){

   if(typeof window !== "undefined"){
       contentBlock = htmlToDraft(html);
   }
   
}

...
}

export default EditorComponent;

eben92 avatar Nov 24 '22 00:11 eben92

The draft-js package has convertFromHTML method which can be used instead of htmlToDraft.

import { convertFromHTML } from 'draft-js';

...

const blocksFromHTML = convertFromHTML(content);

zakariabuet13 avatar Dec 14 '22 08:12 zakariabuet13

import { convertFromHTML } from 'draft-js';

omg Thanks man! 😭 Can't believe I've been wasting my time on this

NG-Joseph avatar Jan 09 '23 21:01 NG-Joseph