简体   繁体   English

React组件构造函数(解析错误:意外令牌,期望“}”)

[英]React component constructor (Parsing error: Unexpected token , expected “}”)

Parsing error: unexpected token, expected } 解析错误:意外令牌,预期}

on the { of constrctor(props){ { of constrctor(props){

I have some other class above this one doing the same thing as constructor(props) but no error 我在这个上面有一些其他类与constructor(props)做同样的事情,但没有错误

if I add } before this class, no matter how many } I added, the error is the same. 如果我在这个课程之前添加} ,无论我添加了多少} ,错误都是一样的。

if I add { before this class, and } after it, it is ok, but the class below this one would show the same error on constructor 如果我在这个类之前添加{}之后,它就可以了,但是这个下面的类会在构造函数上显示相同的错误

After I tried to add {} too surround each class below, it would show 我尝试在下面的每个类中添加{} ,它会显示

Parsing Error: unterminated JSX contents 解析错误:未终止的JSX内容

right before export export

    import React from 'react';
    import ReactDOM from 'react-dom'; 
    import './App.css';
    import $ from 'jquery';

//some classes extends React.Component

class CommentRow extends React.Component {
  constructor(props) {
    super(props);
    this.DeleteComment=this.DeleteComment.bind(this);
  }

  DeleteComment(e) {
    this.props.dComment(e.target._id);
  }

  render() {
    const comment=this.props.comment
    if (comment.name==this.state.name)
        var yourcomment=<tr _id={comment._id} ondoubleclick={this.DeleteComment}> 
        else 
        var yourcomment=<tr _id={comment._id}>
    return (
    {yourcomment}
    <td>comment.time</td>
    <td>comment.name</td>
    <td><p> said: </p></td>
    <td id="comentcontent">comment.content</td>
      </tr>
    );
  }
}

class Profile extends React.Component {
        constructor(props){
            super(props);
            ....
            some functions
        }
    render(){
        return(
            //some more code
        );
    }
}



class FrontApp extends React.Component {
    //constructor
    render(){
        return(
            //some more code
            <Profile
                  //some function
            />
        );
    }
}

export default Frontapp

Is the Class key word upper case in your code? 代码中的Class关键字是大写吗? It should be class . 应该是class The error message is not clear about it but maybe this is the problem. 错误消息不清楚,但可能这是问题所在。

I don't think that way of returning something in your CommentRow 's render function is valid, even though technically the variable yourcomment is the opening <tr> tag it's needing. 我不认为在CommentRow的渲染函数中返回某些东西的方式是有效的,即使从技术上来说,变量yourcomment是它需要的开始<tr>标记。 (You also had a few other small syntax errors in your JSX). (您的JSX中还有一些其他小的语法错误)。

Instead try to have the <tr> there and conditional add props to it, like so: 而是尝试在那里使用<tr>和条件添加道具,如下所示:

render() {
    const { comment } = this.props;
    const { name } = this.state;

    return (
    <tr _id={comment._id} onDoubleClick={comment.name === name && this.DeleteComment}>
       <td>{comment.time}</td>
       <td>{comment.name}</td>
       <td><p> said: </p></td>
       <td id="comentcontent">{comment.content}</td>
    </tr>
    );
  }

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

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