0

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.

3
  • This isn't really an ES6 issue, is it? You're binding a parameter to a function, in any ES*. Commented Oct 15, 2015 at 19:11
  • @Rudie can you explain it in an answer ? please. Commented Oct 15, 2015 at 19:14
  • Where is dealerInfo coming from where you call the function? Commented Oct 15, 2015 at 19:20

2 Answers 2

1

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

<FloatingActionButton onClick={() => this._openAddDealer(dealerInfo)}>
Sign up to request clarification or add additional context in comments.

2 Comments

Philosophically speaking: AWESOME !
Great! This is JSX, right? Let's tag it that way and tighten up the title.
1

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...

1 Comment

did not get the button.onClick. I just did <FloatingActionButton onClick={this._openAddDealer.call(this, dealerInfo)}> and the same happens.

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.