Alex Enrique
Alex Enrique

Reputation: 1

Inject string code in <head> tag in React

I need to inject a string into the tag in a React (+ Vite) application, but I couldn't make it work.

We have a white label app, so the string comes from an API request and should be injected inside the tag. Things like google tag manager scripts and other related stuff, which vary from site (domain) to site, should go here.

As an example, I am receiving from the API the following string which I would like to parse as a react component and insert in the tag:

"<script>\r\n\twindow.addEventListener(\"load\", function () {\r\n\t\talert('test')\r\n\t});\r\n</script>"

I am already using React Helmet but it does not accepts dangerouslySetInnerHTML. Also, tryed this answer, but didn't worked.

Could someone help me with this issue? Thanks in advance.

Upvotes: 0

Views: 104

Answers (0)

Related Questions