Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda