Call external Javascript function from react components

前端 未结 4 1961
南旧
南旧 2020-12-08 14:32

Im not sure if this has been asked before or anybody has encountered the same issue on reactjs. So the scenario is like this, I have an index.html file that include

4条回答
  •  一个人的身影
    2020-12-08 14:34

    Try this solution to call global functions from React with TypeScript enabled:

    Either in index.html or some_site.js

    function pass_function(){
      alert('42');
    }
    

    Then, from your react component:

    window["pass_function"]();
    

    And, of course, you can pass a parameter:

    //react
    window["passp"]("react ts");
    
    //js
    function passp(someval) {
      alert(`passes parameter: ${someval}`);
    }
    

提交回复
热议问题