最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unexpected use of 'open' no-restricted-globals on ReactJS - Stack Overflow

programmeradmin0浏览0评论

I'm using a Modal dependency called 'react-responsive-modal'. It was working perfect but suddenly it stopped and there appeared an error message:

Unexpected use of 'open' no-restricted-globals

This error message is referencing this code line:

<Modal open={open}  onClose={this.onCloseModal} onExited={this.onExited} top>

Does anybody know how to solve it? Thank you

import React, { Component } from 'react'  
import Modal from 'react-responsive-modal';

class Interiores extends Component {
  constructor(props) {
    super(props)
    this.state = {        
      open: false
    }
  }

  onOpenModal = () => {
    this.setState({ open: true });     
  };

  onCloseModal = () => {
    this.setState({ open: false });
  };  
    
  render () {
    return (
      <div>             
        <button onClick={this.onOpenModal}>Open modal</button>
         
        <Modal open={open}  onClose={this.onCloseModal} onExited={this.onExited} top>
          <div>
            <h2>Simple centered modal</h2>
            <p>Text here</p>
          </div>
        </Modal>
      </div>
    );
  }
}

I'm using a Modal dependency called 'react-responsive-modal'. It was working perfect but suddenly it stopped and there appeared an error message:

Unexpected use of 'open' no-restricted-globals

This error message is referencing this code line:

<Modal open={open}  onClose={this.onCloseModal} onExited={this.onExited} top>

Does anybody know how to solve it? Thank you

import React, { Component } from 'react'  
import Modal from 'react-responsive-modal';

class Interiores extends Component {
  constructor(props) {
    super(props)
    this.state = {        
      open: false
    }
  }

  onOpenModal = () => {
    this.setState({ open: true });     
  };

  onCloseModal = () => {
    this.setState({ open: false });
  };  
    
  render () {
    return (
      <div>             
        <button onClick={this.onOpenModal}>Open modal</button>
         
        <Modal open={open}  onClose={this.onCloseModal} onExited={this.onExited} top>
          <div>
            <h2>Simple centered modal</h2>
            <p>Text here</p>
          </div>
        </Modal>
      </div>
    );
  }
}
Share Improve this question edited Feb 13, 2024 at 23:29 Jasperan 4,0556 gold badges28 silver badges62 bronze badges asked Aug 21, 2018 at 2:27 claudiopbclaudiopb 1,1001 gold badge15 silver badges27 bronze badges 3
  • exact but i don't know how to solve it – claudiopb Commented Aug 21, 2018 at 2:34
  • 1 it was probably being used destructured from state. in render() { const { open } = this.state; return ( ... ); } – Derek Commented Aug 21, 2018 at 2:36
  • 1 this.state.open – Andy Ray Commented Aug 21, 2018 at 2:46
Add a ment  | 

1 Answer 1

Reset to default 4

This is because open is a reserved keyword in js .

http://www.javascripter/faq/reserved.htm

I reckon you will be better off destructuring your state in your render method like

const {open} = this.state

But given the intention of the variable, IMHO it would be better to name is as isOpen:false .

Or change the render method as

      <Modal 
        open={this.state.open}
        onClose={this.onCloseModal}
        onExited={this.onExited}
        top
      >
        <div>
          <h2>Simple centered modal</h2>
          <p>Text here</p>
        </div>
      </Modal>
    </div>
  );
}
发布评论

评论列表(0)

  1. 暂无评论