bumpkin bumpkin - 1 year ago 217
React JSX Question

Utilize external script function in React?

I'm trying to load the Twilio Video client-side script into my React + Rails project. In

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?

Answer Source

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 (
          {Object.keys(Twilio.Conversations).join(', ')}
  return App; // mock expoort

// 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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download