Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render original HTML and <script> tag in dom #95

Closed
meesfrenkelfrank opened this issue Mar 13, 2022 · 0 comments
Closed

Render original HTML and <script> tag in dom #95

meesfrenkelfrank opened this issue Mar 13, 2022 · 0 comments

Comments

@meesfrenkelfrank
Copy link

meesfrenkelfrank commented Mar 13, 2022

I know the <script> tag isn't rendered by React dom, as I can read here.

From the api below HTML is returned and I want to add that through the ReactHtmlParser. So by the options its' returning exactly below <script> tag and it's children.

<div class="css-class" data-src="/event/66478667"></div> <script>(function(d, s, id) {var js,ijs=d.getElementsByTagName(s)[0];if(d.getElementById(id))return;js=d.createElement(s);js.id=id;js.src="https://embed.widget.js";ijs.parentNode.insertBefore(js, ijs);}(document, 'script', 'my-js'));</script>

When I try below, the <script> tag is rendered in the dom (with the correct children data), but the actual HTML <div class="css-class" data-src="/event/66478667"></div> isn't rendered anymore?

import ReactHtmlParser, { Transform } from 'react-html-parser';

 const transform: Transform = (node, index) => {
    if (node.type === 'script' && node.children?.length) {
      console.log('Node', node);
      return <script key={index}>{node?.children[0]?.data}</script>;
    }
    return null;
  };

  const options = {
    transform,
  };

return (
    <div>
      {ReactHtmlParser(text, options)}
    </div>
  );
@meesfrenkelfrank meesfrenkelfrank changed the title How getting <script> tag and it's children returned through the ReactHtmlParser Render original HTML and <script> tag in dom Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant