简体   繁体   English

具有React.createClass的组件中的SyntaxError

[英]SyntaxError in component with React.createClass

Im trying to create a function in React which should eventually make some calculations and return the result to my component. 我试图在React中创建一个函数,最终应该进行一些计算并将结果返回给我的组件。 The function isn't done, so bear with me if it doesn't make sense where I'm going with this. 该功能尚未完成,因此如果我不打算使用该功能,请多多包涵。

I'm using React.createClass . 我正在使用React.createClass

The code looks like this 代码看起来像这样

  render: function() {
    return (
      <div className="container">
         <h3 className="time">{this._getTime(1)}</h3>
      </div>
    );
  }

  _getTime(time) {
    if (time === 1) {
      return '1';
    } else if (time === 2) {
      return '2';
    } else {
      return 'stuff';
    }
  }

This just returns a SyntaxError: Unexpected token pointing to the _getTime. 这只是返回一个SyntaxError:指向_getTime的意外令牌。 Any hints to what im doing wrong? 任何暗示我做错了什么?

It looks like you're using React.createClass , which accepts an Object argument. 看起来您正在使用React.createClass ,它接受一个Object参数。

Because it's an Object, you need a comma between each property or shorthand method. 因为它是一个对象,所以您需要在每个属性或速记方法之间使用逗号。

var Hello = React.createClass({
  render: function() {
    return (
      <div className="container">
         <h3 className="time">{this._getTime(1)}</h3>
      </div>
    );
  }, // <--- comma added here

  _getTime(time) {
    if (time === 1) {
      return '1';
    } else if (time === 2) {
      return '2';
    } else {
      return 'stuff';
    }
  }
});

https://jsfiddle.net/pfnfvyb1/ https://jsfiddle.net/pfnfvyb1/

class doesn't use comma delimiters, so this issue is common when switching between the two styles. class不使用逗号分隔符,因此在两种样式之间切换时,此问题很常见。

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

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