Non
Non

Reputation: 8589

How to add the parameter when calling the function?

I am trying to figure out how to call a function which has a param, I am using arrow functions.

Just an example:

_openAddDealer = (dealerInfo) => {
  console.log(dealerInfo);
}

And I need to call that from a button

<FloatingActionButton onClick={this._openAddDealer}>

For the rest of the functions I don't need any bind to this or something similar. So, what can I do to something like this:

<FloatingActionButton onClick={this._openAddDealer(dealerInfo)}>

I tried like that but the function is being called when the app loads and not when the button is pressed.

Upvotes: 0

Views: 30

Answers (2)

JMM
JMM

Reputation: 26827

I assume this is JSX. Likely you're looking for something like:

<FloatingActionButton onClick={() => this._openAddDealer(dealerInfo)}>

Upvotes: 1

Rudie
Rudie

Reputation: 53881

Might it be this simple:

button.onClick = function() {
  // with button's this
  _openAddDealer.call(this, dealerInfo);
  // with outside this
  _openAddDealer(dealerInfo);
};

? I don't know what you're doing with the < etc, so I might misunderstand...

Upvotes: 1

Related Questions