WebStorm中的React.js未解析变量

时间:2018-06-26 09:49:57

标签: javascript reactjs webstorm

在以下代码段中,我遇到了一个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>

IDE在unresolved variable temp中显示一个handleModalView()。但是,我的代码可以正常工作。

编辑1 : 我已经包括了我编写的构造函数。错误仍然存​​在。

1 个答案:

答案 0 :(得分:0)

您应该在组件类中创建一个构造函数并绑定所有成员函数:

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>)
}
相关问题