question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Autobind decorator

See original GitHub issue

I want to use autobind decorator for components.

This https://github.com/andreypopp/autobind-decorator/blob/master/src/index.js work fine with React (method binding). But have issue with RHL. Decorator bind method to instance. When new RHL add new prototype to prototype chain nothing happened, because method already in instance and new method in proto not visible when getter called.

I use ugly hack and check in getter if (this.props) to determine is this – instance.

It is not an issue with RHL or decorator itself. I try to find nice solution.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:26 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
yangmillstheorycommented, Jun 4, 2016

I don’t think this is fully working with react-hot-loader@3.0.0-beta.2 yet.

Small test commit here.

jun-04-2016 12-03-46

In the demo above there’s 3 steps:

  1. without @autobind on onClick, click the button. This doesn’t have the right context - this is null.
  2. add @autobind to onClick - this is recognized by RHL and correct logs the right state in the console (this is bound to the Counter instance).
  3. remove @autobind, this is still bound to the Counter instance (you would expect it not to be).

RHL doesn’t remove the binding when using fat arrows to declare the method either.

Hope that’s helpful, would really love to use this with RHL!

1reaction
slonoedcommented, Jul 16, 2015

For me this issue not actual now. I switch to arrow functions in class definition.

class Cmp extends Component {
  myBindedFunc = () => {
     this.callSmth();
  }
}

Can I close this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

autobind-decorator - npm
Decorator for binding method to an object. Latest version: 2.4.0, last published: 4 years ago. Start using autobind-decorator in your ...
Read more >
andreypopp/autobind-decorator - GitHub
autobind decorator. A class or method decorator which binds methods to the instance so this is always correct, even when the method is...
Read more >
Working of a Autobind Decorator in typescript? - Stack Overflow
Autobind Decorator is an NPM package which binds methods of a class to the correct instance of this , even when the methods...
Read more >
React & Autobinding - Medium
Autobind Decorator is an NPM package which binds methods of a class to the correct instance of `this`, even when the methods are...
Read more >
AutoBind TypeScript decorator - Code Review Stack Exchange
The idea. So first of all, about the idea. Technically it is nice to auto-bind methods and use them without worrying about the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found