11

I'm making a React application and I need to run a Javascript script only in one component, making reference to one of the div's.

I know there are some libraries to do so, but I wonder if there's a more direct way to do it. Right now I do this:

import React, { Component } from "react";
import "../ThisComponent.css";

export default class MyComponent extends Component {
  render() {
    return (
      <div>
        <div id="scriptTarget" />
        <div className="main">
          // Other stuff
        </div>
        <script src="js/myScript.js" />
      </div>
    );
  }
}

But nothing happens. The script is stored in public/js/myScript.js.
Thanks!

3
  • 1
    Possible duplicate of Adding script tag to React/JSX Commented Dec 16, 2018 at 18:50
  • 3
    Why wouldn't you import the script, and then call into it to trigger its behavior on the div from componentDidMount? Commented Dec 16, 2018 at 18:56
  • If he wants to add the script tag to that specific element the mentioned answer won't work. Commented Dec 16, 2018 at 18:56

4 Answers 4

11

I finally solved it by adding the componentDidMount function before rendering the component. Like this:

import React, { Component } from "react";
import "../ThisComponent.css";

export default class MyComponent extends Component {
  componentDidMount() {
    const script = document.createElement("script");

    script.src = "js/myScript.js";
    script.async = true;

    document.body.appendChild(script);
  }

  render() {
    return (
      <div>
        <div id="scriptTarget" />
        <div className="main">
          // Other stuff
        </div>
      </div>
    );
  }
}

If somebody has a better solution please feel free to share it. I'll be aware to further recomendations.

Sign up to request clarification or add additional context in comments.

Comments

4

By using dangerouslySetInnerHTML you can add any valid html you want to your elements.

import React, { Component } from "react";
import "../ThisComponent.css";

export default class MyComponent extends Component {
  render() {
    return (
      <div dangerouslySetInnerHTML="<script src='js/myScript.js' />" >
        <div id="scriptTarget" />
        <div className="main">
          // Other stuff
        </div>
      </div>
    );
  }
}

I should mention though that as the attribute name suggest, its use is strongly discouraged.

Comments

3

export default class MyComponent extends Component {
    componentDidMount() {
        const script = document.createElement("script");
        script.innerHTML = "window.onload = function() {\n" +
            ...
            "}"
        script.async = true;
        document.body.appendChild(script);
    }

    render() {
        return (
            <div>
            </div>
        );
    }
}

Comments

0

You can also use a external component "Helmet" for implement script tags and you can also use link tag,meta tag,style tag etc with the help of this component.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.