1

I created a new component in my Reactjs code and have a button there with onClick function. But the onClick function does not work. I tried everything but it still does not work. Any ideas how I can go about solving this?

import React, { Component } from 'react';
import MenuIcon from '@material-ui/icons/Menu';

class HeaderComponent extends React.Component {
constructor(props){

    super(props);

    this.openMenuBar = this.openMenuBar.bind(this);
}

openMenuBar(){
    console.log("open");
}

render(){
    return(
        <div>
        <button onClick={() => { this.openMenuBar()}}>
                <MenuIcon/>
            </button>
        </div>
    );
}

}

This component is then called in the app.js

<HeaderComponent/>

UPDATE

Some helful info: Whenever I route it to a path (app/path/xx), then the button is not clickable. Otherwise, is it clickable (if the path is root)

3
  • You are mixing both sintaxes. Try onClick={() => this.clickHandler()} or onClick={() => {return this.clickHandler()}} or even onClick={this.clickHandler}. Commented Mar 18, 2019 at 18:57
  • I don't think the code posted by @Thanatos is wrong, because essentially he is declaring an anonymous arrow function, inside which he is referencing and calling openMenuBar. Commented Mar 18, 2019 at 19:03
  • I did everything mentioned in the first comment.. Still not working :/ Commented Mar 18, 2019 at 19:11

2 Answers 2

1

The syntax should be

onClick={this.openMenuBar}

Or

onClick={() => { return this.openMenuBar()}}

Or

onClick={() => this.openMenuBar() }

class MenuIcon extends React.Component {
  render(){
    return <i className="fas fa-plus" />
  }
}
class HeaderComponent extends React.Component {
    constructor(props){
      super(props);
      this.openMenuBar = this.openMenuBar.bind(this);
    }
     
    openMenuBar(){
      console.log("open");
    }

    render(){
      return(
          <div>
            
            <button onClick={this.openMenuBar}> <MenuIcon /></button>
            <button onClick={() => this.openMenuBar()}> btn2</button>
            <button onClick={() => { return this.openMenuBar()}}> btn3</button>
            
          </div>
    );
}
}
ReactDOM.render(<HeaderComponent />, document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

<div id="root"></div>

Sign up to request clarification or add additional context in comments.

5 Comments

tried this.. For some reason the button is not clickable no matter what.. Issue still remains..
I updated the answer, that I can work. I think the problem is the <MenuIcon/>
I added the <MenuIcon /> to test and don't see any issue. You can run my snippet.
This is irrespective of <MenuIcon> . Even after removing it I face the same issue. The button is not clickable no matter what.
I think I got a clue. Whenever I route it to a path (app/path/xx), then the button is not clickable. Otherwise, is it clickabe (if the path is root)
0

try

openmenubar = () => {

...

}

and

<button onClick = { () => this.openmenubar() } > </button>

1 Comment

Can you try without binding function in constructor ? arrow function binds it automatically and can you try it without MenuIcon ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.