简体   繁体   English

WebStorm中的React.js未解析变量

[英]React.js Unresolved variable in WebStorm

I get an unresolved variable issue in the following code snippet: 我在以下代码片段中遇到unresolved variable问题:

constructor(props) {
    super(props);
    this.handleModalView = this.handleModalView.bind(this);
}

handleModalView() {
    this.refs.temp.handleShow();
}

render() {
  return (
    <div className="App">
      <ModalView ref = 'temp' />
      <img src="some_image_source"
        className="SettingsLogo" onClick={this.handleModalView}
      />
  </div>

The IDE shows an unresolved variable temp in handleModalView() . IDE在handleModalView()显示了一个unresolved variable temp However, my code works just fine. 但是,我的代码工作正常。

Edit 1 : I have included the constructor that I have written. 编辑1 :我已经包括了我编写的构造函数。 The error is still there. 错误仍然存​​在。

You should create a constructor in your component class and bind all of the member functions: 您应该在组件类中创建一个构造函数并绑定所有成员函数:

class App extends Component {
    constructor(props) {
      super(props);
      this.handleModalView = this.handleModalView.bind(this);
    }

    handleModalView() {
      this.refs.temp.handleShow();
    }

    render() {
      return (
        <div className="App">
          <ModalView ref = 'temp' />
          <img src="some_image_source"
            className="SettingsLogo" onClick={this.handleModalView}
          />
        </div>)
}

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

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