Javascript 解析错误:意外的标记,预期的“,”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52998987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:02:24  来源:igfitidea点击:

Parsing error: Unexpected token, expected ","

javascriptreactjs

提问by null

I have below code in react js.

我在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 | }

UPDATE

更新

wrapping it up with parent tag returns the same error

用父标签包装它会返回相同的错误

>  9 |       <div>
     |       ^
  10 |         {console.log('nothing')}
  11 |       </div>

UPDATE

更新

here is the whole class

这是全班同学

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;

回答by maxm

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:

返回 jsx 时,您始终需要有一个包装父 JSX 标记。由于您没有任何 jsx 标签,因此它只是无效的 javascript。应该:

class Posts extends Component {
  render() {
    return console.log('test')
  }
}

or if you want the jsx

或者如果你想要 jsx

class Posts extends Component {
  render() {
    return (
      <div>
        {console.log('test')}
      </div>
    )
  }
}

edit (2020-1-19)

编辑 (2020-1-19)

jsx now supports empty tags for parent closures:

jsx 现在支持父闭包的空标签:

class Posts extends Component {
  render() {
    return (
      <>
        {console.log('test')}
      </>
    )
  }
}

回答by Mehrdad Masoumi

in react js You have to put a value in the attr.

在 react js 中,你必须在 attr 中放置一个值。

an example that works

一个有效的例子

<option selected={this.props.info.gender == 0 ? 'selected' : ''} value='0'>men</option>
<option selected={this.props.info.gender == 1 ? 'selected' : ''} value='1'>female</option>

not working

不工作

<option {this.props.info.gender == 0 ? 'selected' : ''} value='0'>men</option>
<option {this.props.info.gender == 1 ? 'selected' : ''} value='1'>female</option>

回答by Andrey Grek

@Mehrdad Masoumi, have a big mistake, this true:

@Mehrdad Masoumi,有一个大错误,这是真的:

<option selected={this.props.info.gender == **1** ? 'selected' : ''} value='**1**'>men</option>
<option selected={this.props.info.gender == **0** ? 'selected' : ''} value='**0**'>female</option>

回答by Ezekiel Saturday

@null use a callback that returns console.log.

@null 使用返回 console.log 的回调。

Asin

阿信

() => console.log('test')

回答by Mehrdad Masoumi

mapStateToProps used by connect method of redux add console.log Before the return method

redux的connect方法使用的mapStateToProps在return方法前添加console.log