0

I have just started working through the book "Reactjs Essentials" from Packt trying to learn react.js, I have everything setup including building my app with gulp. I have never used this before - seems really annoying having to run gulp after every change in the code - but anyway here is some code from the book:

var React       = require('react');
var ReactDOM    = require('react-dom');

var listOfItems =   <ul className="list-of-items">
                        <li className="item-1">Item 1</li>
                        <li className="item-2">Item 2</li>
                        <li className="item-3">Item 3</li>
                    </ul>;

ReactDOM.render(listOfItems, document.getElementById('react-application'));

According to the book, this is ok (the html) and that the JSX compiler will convert this. It saves having to create lots of react elements.

The problem is that gulp complains about an unexpected token that starts at the "<" of the UL tag.

Here is my basic test gulp file also from the book:

gulp.task('default', function () {
    return browserify('./source/app.js')
        .transform(babelify)
        .bundle()
        .pipe(source('snapterest.js'))
    .   .pipe(gulp.dest('./build/'));
});

Up until this point in the book I was creating tags OK with createElement(). This problem has only happened since the book said add the html instead.

Can anyone more experienced see what is wrong, this pretty much the first chapter so I still don't know what could be the cause. I tried searching online but had no joy. I'm guessing it has something to do with the babelify part of the gulp file as that is this JSX compiler part.

Maybe the book is out-of-date or has typos?

Arnold Rimmer
  • 2,723
  • 2
  • 16
  • 23

2 Answers2

0

Looks like Babel believes that your JSX is just JS. Maybe this answer can help. https://stackoverflow.com/a/34162856/3478605

Community
  • 1
  • 1
Valéry
  • 4,574
  • 1
  • 14
  • 25
0

Based on your basic gulp file, it looks like you're forgetting to import Babel.

Be sure to include before your gulp task:

var babelify = require('babelify');

The code itself is perfectly valid, even if it looks a little odd when you're first getting used to JSX: http://codepen.io/anon/pen/VaNYZL

Brad Colthurst
  • 2,515
  • 14
  • 13
  • Thanks. That link helped me fix the problem: I had to install some presets: npm install --save-dev babel-cli babel-preset-react babel-preset-es2015 – Arnold Rimmer May 15 '16 at 06:24