core-decorators icon indicating copy to clipboard operation
core-decorators copied to clipboard

autobind not working with async function

Open JuoCode opened this issue 8 years ago • 9 comments

Use ES7 async function

@autobind
async handleSubmit() {
  this.setState({ loggedIn: true })
}

render() {
  return (
    <button onClick={this.handleSubmit}>Submit</button>
  )
}

Error message

Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined

JuoCode avatar Feb 17 '17 10:02 JuoCode

What compiler are you using?

jayphelps avatar Feb 17 '17 18:02 jayphelps

Use tsc, and compile to target es6

JuoCode avatar Feb 18 '17 09:02 JuoCode

What version of TypeScript? I need as much information as you can give me to help. Also keep in mind that core-decorators does not officially support TypeScript--there are known issues with it.

jayphelps avatar Feb 18 '17 15:02 jayphelps

Typescript: 2.2.0-dev.20170120
core-decorators: 0.15.0
Node: 7.2.0

...core-decorators does not officially support TypeScript...

Working fine in general functions

JuoCode avatar Feb 20 '17 06:02 JuoCode

any news on this?

damianobarbati avatar May 10 '17 13:05 damianobarbati

@damianobarbati No updates from me, but others are welcome to dig in and find the cause. 🤕

jayphelps avatar May 10 '17 20:05 jayphelps

I use this pattern all the time in React Native (babel + browserify) so this error is definitely related to some interaction with TypeScript compiler.

dantman avatar May 17 '17 06:05 dantman

for me it works when running tests ( mocha + node + babel ) but fails on the browser ( webpack + babel )

davidsu avatar Jul 19 '17 13:07 davidsu

Could someone contribute a unit test for this? I'd like to include it in the typescript conversion effort. SEE PR #133.

BurtHarris avatar Aug 04 '17 20:08 BurtHarris