Something is not defined in GatsbyJS

I am using GatsbyJS. Recently I created a file in which I put this code.

import React from 'react'
import { Helmet } from 'react-helmet'

const CustomScript = () => {
    return (
            <script type="application/javascript" src="" async></script>
            var truepush = window.truepush || [];
                    id: "5ec700575558cfe7cca42d1b"
                    if(error) console.error(error);

export default CustomScript

But it gives me this error


Generating development JavaScript bundle failed

  12:17  error  'truepush' is not defined  no-undef

✖ 1 problem (1 error, 0 warnings)

File: src/components/CustomScript.js

failed Re-building development bundle - 5.401s

I have tried couple of things but nothing seems to work. Any help will be really appreciated.

The JS inside your script tag is being evaluated at the time the JSX is built, not as part of the script tag. You usually don’t include script tags in component output except in weird special cases – React just doesn’t work like that. Since this is initialization code, you’d just stick this in the main function that starts your react app, or maybe in a useEffect hook.