0

I can add this file into react component but widget appears under the page. not in the div in render method no style can change its position.

https://www.tradingview.com/widget/market-movers/

  componentWillMount() {
    const script = document.createElement("script");
    const script2 = document.createElement("script");
    script.src =
      "https://s3.tradingview.com/external-embedding/embed-widget-market-overview.js";
    script2.src =
      "https://s3.tradingview.com/external-embedding/embed-widget-hotlists.js";
    script.async = true;
    script2.async = true;
    //script.onload = () => this.scriptLoaded();

    // document.body.appendChild(script);
    // document.body.appendChild(script2);
  }

1 Answer 1

1

Because you are trying to append your new script under body element. You need to use refs and you need to append your new script with using 'componentDidMount'

import React from "react";
import "./styles.css";

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  componentDidMount() {
    const script = document.createElement("script");
    const script2 = document.createElement("script");
    script.src =
      "https://s3.tradingview.com/external-embedding/embed-widget-market-overview.js";
    script2.src =
      "https://s3.tradingview.com/external-embedding/embed-widget-hotlists.js";
    script.async = true;
    script2.async = true;

    this.myRef.appendChild(script);
    this.myRef.appendChild(script2);
  }
  render() {
    return (
      <div
        ref={DOMNodeRef => {
          this.myRef = DOMNodeRef;
        }}
      >
        test
      </div>
    );
  }
}

https://codesandbox.io/s/gracious-noether-qvqgj

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

Comments

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.