Max T
Max T

Reputation: 1455

Use third-party embed code in a React component

I need to use a third part embed code for a form. I'm trying to return it in a component, but it's obviously not working. How can I set this one up?

class Form extends Component {

  render() {

    return (
      <div>
        <div id="apply_form">
          <a name="form" id="formAnchor"></a>
          <script type="text/javascript" src="https://fs22.formsite.com/include/form/embedManager.js?1605047845"></script>
          <script type="text/javascript">
          EmbedManager.embed({
            key: "key",
            width: "100%",
            mobileResponsive: true
          });
          </script>
        </div>
      </div>
    );
  }
}

export default Form;

Upvotes: 6

Views: 5647

Answers (1)

Jacob
Jacob

Reputation: 78880

Unfortunately, there's not a really clean way of doing this, short of reaching into the DOM to insert the external script, then running the embed code directly.

Here's how I'd go about this:

class Form extends Component {

  componentDidMount() {
    const script = document.createElement('script');
    script.setAttribute(
      'src', 
      'https://fs22.formsite.com/include/form/embedManager.js?1605047845');
    script.addEventListener('load', () => {
      EmbedManager.embed({
        key: "key",
        width: "100%",
        mobileResponsive: true
      });
    });
    document.body.appendChild(script);
  }

  render() {
    return (
      <div>
        <div id="apply_form">
          <a name="form" id="formAnchor"></a>
        </div>
      </div>
    );
  }
}

export default Form;

Upvotes: 2

Related Questions