Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Inline condition doesn't load script reactjs nextjs app

I have a problem with a inline condition for loading scripts. The conditions works because the tag shows me de text but for script it doesn't works. How can I solve it.

  const cookie = new Cookies().get('cookiesAccepted');

  const [loadScript,setLoadScript]= useState(cookie);

  function cookieAccepted(){
    setLoadScript(true);
  }
return(
{loadScript &&
      <h1>test</h1>
      }
 <Cookie meta={cookieAccepted.bind(this)}/>
      { loadScript && 
       <script 
       async
       src="https://www.googletagmanager.com/gtag/"
     />
    
      }
      {loadScript &&
        <script 
        dangerouslySetInnerHTML={{
          __html: `
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          gtag('js', new Date());
          gtag('config', '********', {
            page_path: window.location.pathname,
          });
        `,
        }}
      />
      }
)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I recommend using the Script function from next js.

import Script from 'next/script';

Instead of adding tag, you could be doing something like this:

{cookieAccepted && (
  <Script src={`https://www.googletagmanager.com/gtag/`}/>
)}

Check this link for more references: https://nextjs.org/docs/api-reference/next/script

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!