martinoblanco
martinoblanco

Reputation: 5

Pass argument to react method correctly

By means of react I am trying through a click to modify a parameter of a state.

<button onClick={this.switchFase(1)} >Origin</button>

switchFase= (num_proces)=>{
  this.setState({ process: num_proces });
}

I understand that it is not the correct way to do it, can someone explain to me what I am doing wrong?

Thank you!

Upvotes: 0

Views: 32

Answers (1)

mkasp98
mkasp98

Reputation: 359

You should pass a funciton to onClick, instead you are calling this.switchFase(1) immediately and passing its result (undefined) to onClick. Try this:

<button onClick={()=>this.switchFase(1)} >Origin</button>

switchFase= (num_proces)=>{
  this.setState({ process: num_proces });
}

https://reactjs.org/docs/handling-events.html#passing-arguments-to-event-handlers

Upvotes: 1

Related Questions