首页 > 解决方案 > StandardJS linting 返回静态变量的解析错误

问题描述

使用standardJS ,我确实收到了这条线Parsing error: Unexpected token的linting错误。因此我的 CI 失败了。

我不明白这条线有什么问题。我该如何解决这个问题?

export default (App) => {
  return class Apollo extends React.Component {
    static displayName = 'withApollo(App)' // <--
    static async getInitialProps (ctx) {
    // ...
  }
}

标签: javascripteslintstandardjs

解决方案


如果这是标准 javascript,那么错误是类只能包含函数,不能包含属性。

正确的语法应该是:

class Apollo extends React.Component {
  static async getInitialProps (ctx) {
  // ...
}

Apollo.displayName = 'withApollo(App)';

export default (App) => {
  return Apollo;
}

如果您正在使用提议的(但尚未实现或批准)ES7/ES8 类属性,那么可能eslint还不支持它。


如果与原始问题不同,您需要使用App参数,只需在要导出的函数中执行即可:

class Apollo extends React.Component {
  static async getInitialProps (ctx) {
  // ...
}

export default (App) => {
  Apollo.displayName = `withApollo(${App})`;
  return Apollo;
}

推荐阅读