data-client-key inside script tag on React

时间:2019-03-19 14:58:32

标签: reactjs

I do some research about set script on react like this example:

componentDidMount () {
    const script = document.createElement("script");

    script.src = "https://www.example.com/example.js";
    script.async = true;

    document.body.appendChild(script);
}

I need to set data-client-key inside it but seems like react failed to compile it. Can anybody help me out?

1 个答案:

答案 0 :(得分:0)

Try setAttribute

script.setAttribute('data-client-key', 'Your Key')?

Your componentDidMount will look like

 componentDidMount() {
    let s = document.createElement("script");
    s.src = "https://google.com";
    s.setAttribute("data-some-thing", 12);
    document.body.appendChild(s);
 }