Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error -::Type 'string | Element' is not assignable to type 'string'.?

I am using dangerouslySetInnerHTML in my React application (TypeScript based).

I am using it like this:

 <div dangerouslySetInnerHTML={{ __html:  faq.answer  }}/>

I am getting this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Type 'string | Element' is not assignable to type 'string'.

enter image description here

is there any way to fix this?

I am getting HTML from server like this <p>hello</p>. My output is correct. But while creating build my build breaks because we are doing typechecking before build.

>Solution :

You haven’t provided much detail, but, assuming you want to serialize faq.answer in the case that it’s an element, you can use Element.outerHTML like this:

<div dangerouslySetInnerHTML={{ __html: typeof faq.answer === 'string'
  ? faq.answer
  : faq.answer.outerHTML }}/>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading