richb-hanover richb-hanover - 1 month ago 6
React JSX Question

Set up React component to listen to

I want my React component to listen to events from My code works if I have the HTML files include:

<script src=""></script>

and then the component's constructor() has:

this.socket = io(); // eslint-disable-line no-undef

The HTML file retrieves the proper client code from my Express server, and everything's fine.

But this feels so cheesy. I hate to rely on the global window name space to find the io() function. What's the best practice for letting a React component connect through to the server? Thanks.


If you're using a bundler like Webpack or Browserify, you can use

For instance:

const io = require('');

class MyComponent extends React.Component {
  constructor(...args) {
    this.socket = io();