在使用ES6设置初始React状态时,WebStorm检查器会发出警告

Mot*_*rax 5 webstorm ecmascript-6 reactjs babeljs

我一直在研究React网络应用程序,最近从WebStorm 10升级到2016.1.到目前为止,我对新版本感到满意,除了一些检查员的怪癖,如果我能帮助它,我宁愿不在场,其中一个我无法找到解决办法.

我正在使用ES6类来声明我的所有React组件,并且在我设置状态的每个实例中,WebStorm都在.state方法上抛出一个未解决的变量警告.

有问题的代码段:

import React from "react";
import autobind from 'autobind-decorator';

@autobind
class List extends React.Component{
  constructor() {
    super();

    this.state = {
      name: "List",
      items: {},
      history: {},
      suggestions: [],
      highlightIndex: 0,
      suggestionsHover: false,
      autoDelete: true,
      delta: 0,
      mouse: 0,
      isPressed: false,
      lastPressed: 0,
      order: []
    }
  }
Run Code Online (Sandbox Code Playgroud)

警告:

Unresolved variable state
Run Code Online (Sandbox Code Playgroud)

应该注意的是,在WebStorm 10中我没有出现此警告,并且警告会转移到设置状态(即.setState)的其他情况.还应该注意的是,我使用babelify将我的代码转换为ES5以及autobind-decorator,如图所示.

任何帮助或见解将不胜感激.谢谢,如果我需要提供任何其他信息,请告诉我.

Bri*_*ggs 11

尝试使用TypeScript定义文件设置代码帮助.我的意思不是将您的代码转换为TypeScript.看看JetBrains关于这个主题的博文:

https://blog.jetbrains.com/webstorm/2015/10/working-with-reactjs-in-webstorm-coding-assistance/

基本上,进入WebStorm的偏好.向下:Languages & Frameworks > JavaScript > Libraries单击Download按钮.搜索React(以及该列表中可能有帮助的任何其他内容),然后单击Download & Install.这应该有助于删除一些丑陋的警告.

现在,如果只有WebStorm能够将PropTypes应用于props对象,我们就能够获得那些"未解决的变量/函数/方法"警告.