The CreatorCon Call for Content is officially open! Get started here.

UI custom component: How to directly render HTML string from api and keep the styles

Eleven_TW
Tera Contributor

dangerouslySetInnerHTML and 'react-html-parser' are not working to transform the HTML string and showing nothing.

I used the simplest "Hello World" to test the covert but seems like not working.

Eleven_TW_0-1683932847315.pngEleven_TW_1-1683932886036.png

when trying to use react-html-parser with encoded HTML string, it shows the decoded HTML string instead of the component;

Eleven_TW_2-1683933098758.png

 

if use the plain HTML string, it shows the error:
COMPONENT_ERROR_THROWN action type is dispatched with error details. TypeError: Cannot add property elm, object is not extensible

Eleven_TW_3-1683933233200.png

My real HTML string is much more complicated with nested lists, links, etc. I want to render the whole HTML as component so as for further actions like buttons/cards..., so the document.body.appendChild() is not ideal for my case.

Please kindly help!

Thanks

1 REPLY 1

Jesalyn S
Kilo Sage

Hi Eleven_TW,

 

I recommend trying to use the <now-rich-text> component for this use case.

 

https://developer.servicenow.com/dev.do#!/reference/next-experience/utah/now-components/now-rich-tex...

 

Thanks!