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

179
Views
Set contents of <script> in React serverside component

I have this component:

export class Demo extends React.Component<DemoProps, any> {
    private foo: number;

    constructor(props: DemoProps) {
        super(props);
    }

    render() {
        return (
            <html>
            <head>

                <script>
                   // I would like to add an inline script here
                </script>

            </head>
            <body>
            <div id="root">
                (hello world)
            </div>
            <div>
                <progress id="hot-reload-progress-bar" value="100" max="100"></progress>
            </div>
            </body>
            </html>
        )
    }
}

how can I add an inline script inside the <script></script> tags?

If try this:

     getScript() {

        const config = JSON.stringify({
            env: process.env.NODE_ENV
        });

        return 'define(\"@config\", [], function () {' +
            ' return ' + config +';' +
            '});'
       }


       <head>
          <script>{this.getScript()} </script>
       </head>

I get this on the front end:

<html><head><script data-main="/js/main" src="/vendor/require.js"></script><script>define(&quot;@config&quot;, [], function () { return {&quot;env&quot;:&quot;local&quot;};}); </script></head><div><progress id="hot-reload-progress-bar" value="100" max="100"></progress></div><body><div id="root">Initial Home Page</div></body></html>

The browser cannot parse this because I get the: &quot; characters instead of " or '

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The best thing to do is probably use React's inborn facility - dangerouslySetInnerHTML - please see this link to the React docs:

https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml

Read the docs first! then look at my solution below.

The only way I got this feature working was with the following:

export class Demo extends React.Component<DemoProps, any> {

    constructor(props: DemoProps) {
        super(props);
    }

    getScript() {   // return a string representing JS code

        const config = JSON.stringify({
            env: process.env.NODE_ENV
        });

         // return a plain JS object with the __html property
         // set to the string

        return {__html:'define("@config", [], function () {' +
            ' return ' + config +';' +
            '});'}
    }


    render() {

        return (
            <html>

            <head>
                <script dangerouslySetInnerHTML={this.getScript()}/>
            </head>

            <body>
            <div id="root">
            </div>

            </body>
            </html>
        )
    }
}
over 4 years ago · Santiago Trujillo Report

0

Not sure if this would satisfy your needs, but you could put your script contents inside a component method and then call the method from within the jsx

export class Demo extends React.Component<DemoProps, any> {
    private foo: number;

    constructor(props: DemoProps) {
        super(props);
    }

    scriptContents(){
    //contents of the script in here
    }

    render() {
        return (
            <html>
            <head>

                <script>
                   {this.scriptContents()}
                </script>

            </head>
            <body>
            <div id="root">
                (hello world)
            </div>
            <div>
                <progress id="hot-reload-progress-bar" value="100" max="100"></progress>
            </div>
            </body>
            </html>
        )
    }
}
over 4 years ago · Santiago Trujillo 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!