简体   繁体   English

React渲染错误

[英]Error with React render

I'm learning React/Redux from Wes Bos' tutorial . 我正在从Wes Bos的教程中学习React / Redux。 I'm trying to render some html, and there's an error building it every time: 我正在尝试渲染一些html,每次构建它时都会出错:

    ERROR in ./client/reduxstagram.js
Module build failed: SyntaxError: /var/www/learn-redux/client/reduxstagram.js: Unexpected token (8:7)
  6 | import css from './styles/style.styl';
  7 | 
> 8 | render(<p>hi</p>, document.getElementById('root'));
    |        ^
    at Parser.pp.raise (/var/www/learn-redux/node_modules/babylon/lib/parser/location.js:22:13)
    at Parser.pp.unexpected (/var/www/learn-redux/node_modules/babylon/lib/parser/util.js:89:8)
    at Parser.pp.parseExprAtom (/var/www/learn-redux/node_modules/babylon/lib/parser/expression.js:517:12)
    at Parser.pp.parseExprSubscripts (/var/www/learn-redux/node_modules/babylon/lib/parser/expression.js:272:19)
    at Parser.pp.parseMaybeUnary (/var/www/learn-redux/node_modules/babylon/lib/parser/expression.js:252:19)
    at Parser.pp.parseExprOps (/var/www/learn-redux/node_modules/babylon/lib/parser/expression.js:183:19)
    at Parser.pp.parseMaybeConditional (/var/www/learn-redux/node_modules/babylon/lib/parser/expression.js:165:19)
    at Parser.pp.parseMaybeAssign (/var/www/learn-redux/node_modules/babylon/lib/parser/expression.js:128:19)
    at Parser.pp.parseExprListItem (/var/www/learn-redux/node_modules/babylon/lib/parser/expression.js:1032:16)
    at Parser.pp.parseCallExpressionArguments (/var/www/learn-redux/node_modules/babylon/lib/parser/expression.js:348:20)

This is my reduxstagram.js file: 这是我的reduxstagram.js文件:

import React from 'react';

import { render } from 'react-dom';

// Import css
import css from './styles/style.styl';

render(<p>hi</p>, document.getElementById('root'));

Has anyone run into a similar problem, or know what I'm doing incorrectly? 有没有人遇到类似的问题,或者知道我做错了什么?

I resolved with this configuratoin chunk for the javascript files: 我用此configuratoin块解析了javascript文件:

// js
{
  test: /\.js$/,
  loader: 'babel',
  query: {
    presets: ['es2015', 'react']
  },
  include: path.join(__dirname, 'client')
}

You didn't setup webpack with the proper loaders to compile jsx, hence the Unexpected token . 您没有使用适当的加载程序来设置webpack来编译jsx,因此没有使用Unexpected token If you're new to webpack and javascript transpilers/compilers, start using something like jsbin and switch from Javascript to ES6 / Babel mode: 如果您不熟悉Webpack和javascript编译器/编译器,请开始使用jsbin之类的东西,然后从Javascript切换到ES6 / Babel模式:

这里

After you've got used to React and the jsx syntax, try learning how to setup webpack with babel. 在习惯了React和jsx语法之后,尝试学习如何使用babel设置webpack。

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

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