[英]ReactJS :Syntax error: src/App.js: Unexpected token (16:6)
I'm getting started with reactjs and tried a simple example in a tutorial.Syntax error occurs when returning html tags. 我开始使用reactjs,并在教程中尝试了一个简单的示例。返回html标记时会出现语法错误。
Here is the error i'm getting 这是我遇到的错误
./src/App.js Syntax error: C:/Users/react-tutotial/src/App.js: Unexpected token (16:6) ./src/App.js语法错误:C:/Users/react-tutotial/src/App.js:意外令牌(16:6)
14 | render() {
| return{
16 | <div>
| ^
17 | <button>Increament</button>
18 | {this.state.count}
this is the app.js file 这是app.js文件
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor(){
super();
this.state = {
count : 0
};
}
render() {
return{
<div>
<button>Increment</button>
{this.state.count}
</div>
};
}
}
export default App;
You should use (
/ )
there, not {
/ }
: 您应该在此处使用(
/ )
,而不要在{
/ }
:
render() {
return ( // changed this line
<div>
<button>Increment</button>
{this.state.count}
</div>
); // changed this line
}
And know that ()
s there are optional, but if you do remove them, <div>
must be in the same line as return
, like return <div>
an on. 并且知道()
是可选的,但是如果您删除它们,则<div>
必须与return
在同一行,例如return <div>
on。 (When you break a line after return
JavaScript goes nuts.) (当return
后return
行时,JavaScript无效。)
Edit: 编辑:
Ok, I feel like I should elaborate on what "goes nuts" is: when you break a line right after return
, JavaScript will basically consider it a return;
好的,我觉得我应该详细说明什么是“疯了”:当您在return
之后立即中断一行时,JavaScript基本上会认为它是return;
and ignore the rest. 忽略其余部分。 And that is effectively the same as return undefined;
这实际上与return undefined;
相同return undefined;
. 。 Blame this on JavaScript's ASI (Automatic Semicolon Insertion) "feature". 将此归咎于JavaScript的ASI(自动分号插入) “功能”。
About why exactly {
/ }
doesn't work: Although when inside a JSX "expression" you can use curly braces to place any JavaScript expression , at that point where the error appears, you are not inside a JSX expression yet. 关于到底为什么{
/ }
不起作用:虽然当JSX“表达” 里面 ,你可以使用大括号放置任何JavaScript表达式 ,在这一点上,其中出现的错误,你就不是一个JSX表情里面呢。 This makes that code be interpreted as regular JavaScript expression. 这使得该代码被解释为常规JavaScript表达式。 And in regular JavaScript, a curly brace alone either creates a context (where let
variables would be local, for instance) or object literals. 在常规JavaScript中,仅花括号就可以创建上下文(例如, let
变量为局部变量)或对象文字。 A return expects an expression. 返回值需要一个表达式。 A context is not an expression. 上下文不是表达式。 And your code is not (nor it should be) a valid object literal. 而且您的代码不是(也不应该是)有效的对象文字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.