title: React - binding `this` to an imported function
body: <p>In my React app, I have a handful of functions that I'd like to be able to access across a few similar components... however, I want to bind <code>this</code> to the shared functions so that they can do things like update the component state, etc... however, it seems that importing the functions and then trying to bind <code>this</code> in the 'typical' React manner does not work.</p>
  
  <p>Here's an illustration of what I'd like to accomplish - in this case, clicking the rendered button would call the function from the imported shared function file and update the component state:</p>
  
  <pre><code>//shared_functions.js
  const sharedFunctions = {
      testFunction = () =&gt; {
          this.setState({functionWasRun: true})
      }
  }
  
  
  //MyComponent.jsx
  import React, { Component } from 'react';
  import sharedFunctions from '../static/scripts/shared_functions.js';
  let { testFunction } = sharedFunctions;
  
  class MyComponent extends Component {
      constructor(props){
          super(props);
          this.testFunction = this.testFunction.bind(this)
          this.state = {
              functionWasRun: false
          }
      }
  
      render(){
          return(
              &lt;div&gt;
                  &lt;button onClick={this.testFunction}&gt;Click&lt;/button&gt;
              &lt;/div&gt;
          )
      }
  }
  </code></pre>
  
  <p>Trying to run this code as is will return an error like:</p>
  
  <p><code>Uncaught (in promise) TypeError: Cannot read property 'bind' of undefined</code></p>
  
  <p>and I get what that's all about... but what I'd like to know is: is it possible to bind <code>this</code> to an <em><strong>imported</strong></em> function? </p>
  
  <p>I'm starting to get a lot of similar-looking functions popping up throughout my app and I'd love to simplify things by abstracting them into a shared script, but I'm not sure how to achieve the typical <code>this</code> binding that's needed to achieve state-setting.</p>