DEV Community

Discussion on: How to Bind 'this' in React Without a Constructor

Collapse
joelnet profile image
JavaScript Joel

In your "messy" example, I believe the code should be even messier. The bind won't mutate the original function, but will return a new and bound function. So you must also set this to overwrite the original function.

class SomeCompenent extends React.Component {
   constructor() {
    super();
    // this does not modify the original function
    this.renderSomeInput.bind(this);

    // this binds the original function
    this.renderSomeInput = this.renderSomeInput.bind(this);
   }
}