4

When I uses babel to watch a jsx file. But there is a syntax error.

Before that, I uses react-tools to watch, and everything is fine.

SyntaxError: assets/js/chat/chat.jsx: Unexpected token (258:16)
  256 |         if (this.props.isOpen) {
  257 |             return (
> 258 |                 <div className="modal-overlay">
      |                 ^
  259 |                     <ReactCSSTransitionGroup transitionName={this.props.transitionName}>
  260 |                         <div className="chat-modal">
  261 |                             {this.props.children}

The following is my code.

var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;
var Modal = React.createClass({
    render: function() {
        if (this.props.isOpen) {
            return (
                <div className="modal-overlay">
                    <ReactCSSTransitionGroup transitionName={this.props.transitionName}>
                        <div className="chat-modal">
                            {this.props.children}
                        </div>
                    </ReactCSSTransitionGroup>
                </div>
            )
        } else {
            return <div className="modal-overlay"><ReactCSSTransitionGroup transitionName={this.props.transitionName}/></div>
        }
    }
});
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
augustine
  • 538
  • 4
  • 15
  • it looks fine, what is this ReactCSSTransitionGroup? maybe the error is within that component. can you try removing it and see if it throws the same error? – Chris Hawkes Nov 12 '15 at 16:23
  • ReactTransitionGroup is an add-on component that react provides. https://facebook.github.io/react/docs/animation.html I will try to remove it. – augustine Nov 13 '15 at 04:10

3 Answers3

4

I had a similar issue the other day. It seems that babel now needs some additional plugins to work with react.

See the answer in this SO question: babel-loader jsx SyntaxError: Unexpected token

Community
  • 1
  • 1
Tom
  • 2,321
  • 2
  • 18
  • 29
4

command:

babel --watch assets/js/ --out-dir dist/js/ --presets react

or package.json:

{
    "name": "myweb",
    "version": "1.0.0",
    "babel": {
        "presets": ["react"]
    }
}
augustine
  • 538
  • 4
  • 15
  • Thanks. I added this in the module/rules/options/presets of webpack.config.js, rather than package.json - but it's working - hooray! So many reports of this problem, and different solutions seem to work for each case. Bizarre. – JosephK May 02 '19 at 17:09
1

Which version of babel you have? If you upgraded to 6, you have to add react preset...

{
    test: /\.js$/,
    exclude: /node_modules/,
    loader: "babel",
    query:
    {
      presets:['react', 'es2015', 'stage-0']
    }
  }
jirikolarik
  • 1,243
  • 1
  • 13
  • 25
  • 1
    babel version is 6.1.18. I have installed babel-preset-react. But I found the problem, I didn't add "--presets" options. – augustine Nov 16 '15 at 03:25