1

I have a Login component in my header(bootstrap) which needs to open a Modal when I click. I am using the Modal from react-modal (npm package). Even though I am not getting an error the popup won't open.

this.state={
    openModal:false
}

closeModal=()=>{
    this.setState({openModal:false})
}

 handleOnClick=()=>{
     this.setState({openModal:true}
 }

Header Component

` <ul className="nav navbar-nav navbar-right">
                    <li onClick={this.handleOnClick()}><a href="#">
                    <Modal 
                        isOpen={this.state.openModal}
                        onRequestClose={this.closeModal}
                        style={customStyles}
                        contentLabel="Example Modal"
                    >
                    <span class="glyphicon glyphicon-user"></span></Modal> Sign Up</a></li>

                    <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
                </ul>`
Erick
  • 1,098
  • 10
  • 21

2 Answers2

1

You need to remove the brackets () from the call. What you give to onClick is the function to call, not the code to run like in other frameworks like Angular.

So, that'll be onClick={this.handleOnClick}

Meligy
  • 35,654
  • 11
  • 85
  • 109
0

This is the working code.

   <ul className="nav navbar-nav navbar-right">
                            <li  onClick={this.handleOnClick}><a href="#">
                            <Modal 
                                isOpen={this.state.openModal}
                                onRequestClose={this.closeModal}
                                style={customStyles}
                                content

                            ><LoginComponent />
                        </Modal> Login</a></li> 
Erick
  • 1,098
  • 10
  • 21