DEV Community

Ibrahim EL-Sanosi
Ibrahim EL-Sanosi

Posted on

Render HTML and script in React

I have HubSpot call-to-action embed code. I would like to inject it in my React page.

The code call-to-action embed code is:

<!--HubSpot Call-to-Action Code --><span class="hs-cta-wrapper" id="*******"><span class="*******" id="********"><!--[if lte IE 8]><div id="hs-cta-ie-element"></div><![endif]--><a href="********" target="_blank" rel="noopener"><img class="hs-cta-img" id="**********" style="border-width:0px;" height="20" width="20" src="**************"  alt="New call-to-action"/></a></span><script charset="utf-8" src="https://js.hscta.net/cta/current.js"></script><script type="text/javascript"> hbspt.cta.load(*****, '********', {"useNewLoader":"true","region":"na1"}); </script></span><!-- end HubSpot Call-to-Action Code -->
Enter fullscreen mode Exit fullscreen mode

I want to embded code in React.

Attempts:

I use dangerouslysetinnerhtml but this does not seem working for script.

const cta = " embed code here ";

Enter fullscreen mode Exit fullscreen mode

Then I use dangerouslySetInnerHTML attribute on an HTML element to add an CTA HTML string inside its content as follow:

 dangerouslySetInnerHTML={{__html: cta}}

Enter fullscreen mode Exit fullscreen mode

Using dangerouslySetInnerHTML, it does not work for script tag.

How can I achieve this, rendering the above embed code without missing any part of it?

Top comments (1)

Collapse
 
_bkeren profile image
''