andreypopp / autobind-decorator

Decorator to automatically bind methods to class instances
MIT License
1.45k stars 66 forks source link

"Cannot set property" error on React class component method #32

Closed AndrewRayCode closed 7 years ago

AndrewRayCode commented 8 years ago

I'm guessing I'm missing something but not sure what.

My .babelrc:

{
  "presets": ["react", "es2015", "stage-0"],

  "plugins": [
    "transform-decorators-legacy"
  ],

  "env": {
    "development": {
      "plugins": [ "typecheck" ]
    }
  }
}

My webpack loader config:

{
    test: /\.jsx?$/,
    exclude: /node_modules/,
    loaders: [ 'react-hot', 'babel?presets[]=es2015&presets[]=react&presets[]=stage-0&plugins[]=transform-decorators-legacy' ]
}

My .eslintrc (I have no idea if both of these are needed):

  "parser": "babel-eslint",
  "settings": {
    "import/parser": "babel-eslint",

In my React component, I use the autobind decorator:

import autobind from 'autobind-decorator';

export default class Home extends Component {

    @autobind
    onPress() {}

}

But I get the runtime error:

makeAssimilatePrototype.js:21 Uncaught TypeError: Cannot set property onPress of # which has only a getter

InfernoZeus commented 8 years ago

Just started getting this issue as well - no idea what's triggered the change. The only thing I did was extract a class into its own file, but I don't think that caused it.

AndrewRayCode commented 8 years ago

@InfernoZeus what version of React are you using? Wondering if component classes changed something internally recently. Or maybe how Babel handles classes?

For me: React 15.2.1 babel-core 6.11.4

InfernoZeus commented 8 years ago

I'm using the same version of React, and a slightly earlier version of babel-core: 6.10.4

VinceFanZ commented 8 years ago

I've had the same problem.

VinceFanZ commented 8 years ago

user import { autobind } from 'core-decorators'

smashercosmo commented 8 years ago

https://github.com/gaearon/react-transform-catch-errors/issues/26 probably related

alexilyaev commented 8 years ago

@VinceFanZ Did not work for me with core-decorators

Jinnified commented 8 years ago

@smashercosmo if my dependency's dependency has react-transform-catch-errors and i have transform-decorators-legacy, could this still causing issues?

alexilyaev commented 8 years ago

This might be related:
https://github.com/jayphelps/core-decorators.js/issues/48#issuecomment-200592851

iamricky commented 7 years ago

I'm still receiving the Uncaught TypeError: Cannot set property render of #<ComponentName> which has only a getter when binding to the class, e.g.

@autobind
class Component { }

Has this issue been addressed? Please advise.

source: https://github.com/gaearon/react-transform-catch-errors/issues/26#issuecomment-197078851

AndrewRayCode commented 7 years ago

I think this project is abandonware that will only work with older versions of something (babel? react?)

stevemao commented 7 years ago

See https://github.com/andreypopp/autobind-decorator/pull/29

silent-tan commented 7 years ago

met the same problem, anything else solution?

smashercosmo commented 7 years ago

I fixed it long time ago in react-transform-catch-errors https://github.com/gaearon/react-transform-catch-errors/commit/afbbda89959e886e636d00427f20b6efc80bf441 It just wasn't published. Installing directly from master should work for everyone who faced this issue.

stevemao commented 7 years ago

98968ee should have fixed this. Please try the latest master branch :)

alexilyaev commented 7 years ago

With react-hot-loader 3 this worked with version v1.4.1 as well.
Tested now with v2.1.0, still works.