简体   繁体   中英

How to Embed 3rd Party JS scripts in React?

I am trying to embed a video player from Chew.tv in my react component. The following code is what I have so far (it doesn't work).

import React, {Component} from 'react';


export default class Chew extends Component {
    componentDidMount() {
        const script = document.createElement('script');
        script.setAttribute(
            'src',
            'https://asset-cdn.chew.tv/js/embed.js');
        document.body.appendChild(script);
    }
    render(){
        return(
            <div className="chew" data-uri={this.props.url} data-autostart="true"/>
        )
    }

}

I've tried googling quite a bit but I can't tell if there is a standard way of doing this. My project is currently using create-react-app and I am looking for a solution that doesn't require me to eject it.

You can directly import it in indix.js

<script src="https://asset-cdn.chew.tv/js/embed.js" type="text/javascript"></script>

Its functionalities will be accessible at componentDidMount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM