You can't because script tags are removed automatically for security.
The best way to work with javascript is to get the string separately and execute (or eval) it from componentWillMount or componentDidMount
class Page extends Component {
componentDidMount() {
const jsCode = `
console.log('hello world');
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'viewCart'
})
`;
new Function(jsCode)();
}
// you can do something else here
render() {
return (
<noscript />
);
}
}
You can obviously use any string. I presume you might be loading it from the server.
In the example above I used new Function()()
which is very similar to eval()
but works faster.
I used componentDidMount
so i'm certain the script executes after the view is shown. Another difference between Will and Did mount is that Will mount executes on both server and client sides of universal (isomorphic) apps, whereas Did mount will only execute on the client side of universal apps.