简体   繁体   English

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

[英]Parsing error: Unexpected token, expected ","

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;

You always need to have a wrapping parent JSX tag when returning jsx.返回 jsx 时,您始终需要有一个包装父 JSX 标记。 Since you don't have any jsx tags it's just invalid javascript.由于您没有任何 jsx 标签,因此它只是无效的 javascript。 Should be:应该:

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')}
      </>
    )
  }
}

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>

@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>

@null use a callback that returns console.log. @null 使用返回 console.log 的回调。

Asin阿信

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

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

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

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