简体   繁体   English

用VScode编辑器做出反应

[英]React with VScode Editor

I am having some issues when I started coding on VScode with React. 我开始使用React在VScode上编码时遇到一些问题。

According to the ReactJS docs, 根据ReactJS文档,

Declaring Default Props With functions and ES6 classes defaultProps is defined as a property on the component itself: 声明默认属性使用函数和ES6类,defaultProps被定义为组件本身的属性:

class Greeting extends React.Component {
 // ...
}

Greeting.defaultProps = {
  name: 'Mary'
};

I tried to follow and this is my code: 我试图遵循,这是我的代码:

class Records extends React.Component {

constructor(props) {
  super (props);
  this.state = {records: props.data};
      }

  Records.defaultProps = {
  records: []
};

I have this error when I type this code in VScode. 在VScode中键入此代码时出现此错误。

 [js] ';' expected

Is my code of .defaultProps wrong or is this a VScode thing? 我的.defaultProps代码是错误的还是这是VScode? thanks! 谢谢!

If you want to define your defaultProps within the React component class, you need to define it with the Static keyword like 如果要在React组件类中定义defaultProps,则需要使用Static关键字进行定义,例如

class Records extends React.Component {

constructor(props) {
  super (props);
  this.state = {records: props.data}
}

  static defaultProps = {
     records: []
  };

or else declare it like 否则就这样声明

class Records extends React.Component {

  constructor(props) {
    super (props);
    this.state = {records: props.data};
  }   
}

Records.defaultProps = {
  records: [];
};

Also in your VSCODE you may have a user setting to have a termination ; 同样,在您的VSCODE中,您可能具有用户设置来终止; in each line 在每一行

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

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