解析错误:意外的令牌,预期为“,"; [英] Parsing error: Unexpected token, expected ","
本文介绍了解析错误:意外的令牌,预期为“,";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在react js中有以下代码.
I have below code in react js.
class Posts extends Component {
render() {
return (
{console.log('test')}
);
}
}
运行此代码后,出现错误
After running this code I get error which is
Parsing error: Unexpected token, expected ","
8 | return (
> 9 | {console.log('test')}
| ^
10 | );
11 | }
12 | }
更新
用父标记将其包装起来会返回相同的错误
wrapping it up with parent tag returns the same error
> 9 | <div>
| ^
10 | {console.log('nothing')}
11 | </div>
更新
这是整个班级
import React, { Component } from 'react';
import { connect } from 'react-redux';
class Post extends Component {
render() {
return (
<div>
{console.log('test')}
</div>
);
}
}
const mapStateToProps = (state) => {
return {
posts: state
}
};
export default Post;
推荐答案
返回jsx时,您始终需要包装父JSX标签.由于您没有任何jsx标记,因此只是无效的javascript.应该是:
You always need to have a wrapping parent JSX tag when returning jsx. Since you don't have any jsx tags it's just invalid javascript. Should be:
class Posts extends Component {
render() {
return console.log('test')
}
}
或者如果您想要jsx
or if you want the jsx
class Posts extends Component {
render() {
return (
<div>
{console.log('test')}
</div>
)
}
}
编辑(2020-1-19)
edit (2020-1-19)
jsx现在支持父闭包的空标签:
jsx now supports empty tags for parent closures:
class Posts extends Component {
render() {
return (
<>
{console.log('test')}
</>
)
}
}
这篇关于解析错误:意外的令牌,预期为“,";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文