abu abu
abu abu

Reputation: 7022

Error in onClick event in react.js

I am trying to use onclick event like below

<button className="mini ui button" onClick={this.view(item['id'])}>
   <i className="user icon"></i>
   View
</button>

This is creating error like below

enter image description here

But below code is not creating error

<button className="mini ui button" onClick={this.view}>
     <i className="user icon"></i>
     View
</button>

Upvotes: 0

Views: 533

Answers (2)

Laura delgado
Laura delgado

Reputation: 362

Yes you can use arrow function as previews answer or you can use bind

<button className="mini ui button" onClick={this.view.bind(this, item['id'])}>
   <i className="user icon"></i>
   View
</button> 

These two ways works.

Upvotes: 0

Roy Wang
Roy Wang

Reputation: 11260

Change it to onClick={() => this.view(item['id'])}, otherwise the function gets executed immediately during rendering (instead of onClick), which causes repeated re-rendering if the function changes the state/props.

Upvotes: 3

Related Questions