1

I am trying to generate click event for button. I have put button in variable to map different buttons for service but click event is not working. Its giving "Cannot read property 'addCompnay' of undefined.". Can someone help me with this.

   class Sidebar extends React.Component {

    constructor(props) {

       super(props);

       this.state = {

          allServices:[],

       }

       this.addCompnay = this.addCompnay.bind(this);


  }

  addCompnay() {

      alert("asd");

  }

  render() {

  var sidebarpagedata = this.state.allServices.companies.map(function(sidebarpagedata, index){
<Button type="button" className="default pull-right" onClick={this.addCompnay} >Apply Now</Button>

 })

 return (

  {sidebarpagedata}

 );

}

3 Answers 3

2

You got 2 problems with this code:

  var sidebarpagedata = this.state.allServices.companies.map(function(sidebarpagedata, index){
<Button type="button" className="default pull-right" onClick={this.addCompnay} >Apply Now</Button>

 })

Your map function is has a different this context, either user an arrow function or pass the this explicitly:

var sidebarpagedata = this.state.allServices.companies.map((sidebarpagedata, index) => {
<Button type="button" className="default pull-right" onClick={this.addCompnay} >Apply Now</Button>

 })

The second problem here is that you are missing a return, change it to this:

var sidebarpagedata = this.state.allServices.companies.map((sidebarpagedata, index) => {
 return (<Button type="button" className="default pull-right" onClick={this.addCompnay} >Apply Now</Button>)

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

1 Comment

because of this code, Click event is generated even if we have not clicked on element. While page scroll or rendering event is called automatically
1

Use

  render() {
     var sidebarpagedata = this.state.allServices.companies.map((sidebarpagedata, index) => {
         <Button type="button" className="default pull-right" onClick={this.addCompnay} >Apply Now</Button>
    })

    return (
        {sidebarpagedata}
    );

}

2 Comments

What you have changed ?
because of this code, Click event is generated even if we have not clicked on element. While page scroll or rendering event is called automatically
0

In the following case you don't need to bind the function if using an arrow function.

class Sidebar extends React.Component {

    constructor(props) {

       super(props);

       this.state = {

          allServices:[],
       }
  }

  addCompnay = () => {
      alert("asd");
  }

  render() {

  var sidebarpagedata = this.state.allServices.companies.map(function(sidebarpagedata, index){
<Button type="button" className="default pull-right" onClick={() => this.addCompnay()} >Apply Now</Button>

 })

 return (

  {sidebarpagedata}

 );

}

Comments

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.