react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

Syntax error when set defaultProps for components

Open hieuhlc opened this issue 10 years ago • 4 comments

Help us help you! Please choose one:

  • [x] My app crashes with react-rails, so I've included the stack trace and the exact steps which make it crash.
  • [ ] My app doesn't crash, but I'm getting unexpected behavior. So, I've described the unexpected behavior and suggested a new behavior.
  • [ ] I'm trying to use react-rails with another library, but I'm having trouble. I've described my JavaScript management setup (eg, Sprockets, Webpack...), how I'm trying to use this other library, and why it's not working.
  • [ ] I have another issue to discuss.

(Describe your issue here)

I wrote a simple component and I want to set its defaultProps. But this weird error occurred. How am I supposed to deal with this?

screen shot 2016-07-04 at 17 11 56

Thank you very much.

hieuhlc avatar Jul 04 '16 10:07 hieuhlc

I suspect that this requires a different Babel setting than the default in react-rails. You could try customizing BabelTransformer https://github.com/reactjs/react-rails/blob/master/lib/react/jsx/babel_transformer.rb

Sorry, I use createClass so I don't have much experience here! 😬

rmosolgo avatar Jul 04 '16 15:07 rmosolgo

You need Babel stage: 0 for static properties to work on ES6 Classes. We use stage2 and write ours like this.

class Foo extends Component {}

Foo.propTypes = {};
Foo.defaultProps = {};

export default Foo;

coryasato avatar Jul 06 '16 19:07 coryasato

Thanks @coryasato ! Stage 2 is safer than stage 0 right? I will use stage 2.

hieuhlc avatar Jul 07 '16 04:07 hieuhlc

Not clear to me, how do I add stage2 to react-rails?

carlows avatar Oct 17 '16 20:10 carlows