简体   繁体   English

React - react.render上的未处理错误事件()

[英]React - Unhandled Error Event on react.render()

I've seem to got a problem compiling my JSX files to JS when I run gulp. 当我运行gulp时,我似乎遇到了将JSX文件编译为JS的问题。

Gulp Dependencies: Gulp依赖:

var gulp        = require('gulp');
var browserify  = require('browserify');
var babelify    = require('babelify');
var source      = require('vinyl-source-stream');
var uglify      = require('gulp-uglify');
var rename      = require('gulp-rename');
var runSequence = require('run-sequence');

I thought that babelify would compile my JSX for me and I wouldn't have to worry about that. 我认为babelify会为我编译我的JSX,我不必担心。

I've then got my build files: 然后我得到了我的构建文件:

gulp.task('build', function () {
  return browserify({
    entries: 'app.js',
    extensions: ['.jsx'],
    debug: true
  })
  .transform(babelify)
  .bundle()
  .pipe(source('bundle.js'))
  .pipe(gulp.dest('dist'));
});

gulp.task('compress', function() {
  return gulp.src('./dist/bundle.js')
    .pipe(uglify())
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest('dist'));
});

gulp.task('default', function(cb) {
  runSequence('build','compress', cb);
});

I seem to be getting the error on my app.js file where I add my first component. 我似乎在我的app.js文件中收到错误,我添加了我的第一个组件。

Appreciate the help in advanced. 感谢先进的帮助。

**** UPDATE **** ****更新****

This is the error that I get once running gulp: 这是我在运行gulp后得到的错误:

events.js:85
      throw er; // Unhandled 'error' event
            ^
SyntaxError: /Users/maxlynn/Documents/childcare-army/app.js: Unexpected token (6:4)
  4 | 
  5 | React.render(
> 6 |     <AppRoot />,
    |     ^
  7 |     document.getElementById('app-root')
  8 | );

If you recently installed babelify you need to pass some additional configuration to the transform call since Babel 6.x no longer ships with default presets enabled. 如果您最近安装了babelify,则需要将一些其他配置传递给转换调用,因为Babel 6.x不再提供默认预设。

In order to be able to transpile JSX you need to enable the react preset. 为了能够转换JSX,您需要启用react预设。

npm install babel-preset-react babel-preset-es2015 --save

.transform('babelify', { presets: ['es2015', 'react'] }) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM