Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add javascript code snippet in react-helmet?

I am using react-helmet to push elements in the head. The code snippet that needs to be used is of GTM and e-commerce tracking. But, I'm not able to find a way to insert script in head anyway. The documentation shows for loading from external source. The code snippet has dynamic values that are being used.

The documentation is from tthis url : react-helmet

<Helmet script={[
                {src: "http://include.com/pathtojs.js", type: "text/javascript"},
                {type: "application/ld+json", innerHTML: `{ "@context": "http://schema.org" }`}
            ]}
/>
like image 248
Abhishek Dhanraj Shahdeo Avatar asked Oct 29 '22 13:10

Abhishek Dhanraj Shahdeo


1 Answers

The documentation is correct. This works

<Helmet 
  script={[{ 
    type: 'text/javascript', 
    innerHTML: 'console.log("It works!")' 
  }]} />

Check your version of Helmet - the innerHTML attribute was introduced in Helmet 3.0.0

like image 50
davnicwil Avatar answered Nov 02 '22 23:11

davnicwil