简体   繁体   中英

Utilize external script function in React?

I'm trying to load the Twilio Video client-side script into my React + Rails project. In index.html I have these two external scripts from Twilio:

<script src="https://media.twiliocdn.com/sdk/js/conversations/v0.13/twilio-conversations.min.js"></script>

I can't figure out for the life of me how to access Twilio from a React component so that I can begin to utilize this script in my app. Must be a no-brainer way, right?

Twilio is accessible on the global scope if you include it as a script tag in your index.html :

 // App.js const App = (function(Component) { // mock imports class App extends Component { render() { // Twilio globally accessible return ( <pre> {Object.keys(Twilio.Conversations).join(', ')} </pre> ); } } return App; // mock expoort })(React.Component); // index.js (function(App, render) { // mock imports render(<App />, document.getElementById('app')); })(App, ReactDOM.render); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script> <script src="//media.twiliocdn.com/sdk/js/conversations/v0.13/twilio-conversations.min.js"></script> <div id="app"></div> 

Maybe clearer example: http://www.webpackbin.com/4JhaBpybf

unminified twilio-conversations.js :

(function(root) {

   ...

  if (typeof define === 'function' && define.amd) {
    define([], function() { return Conversations; });
  } else {
    var Twilio = root.Twilio = root.Twilio || {};
    Twilio.Conversations = Twilio.Conversations || Conversations;
  }
})(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : this);

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